SSM集成
导包
数据源配置文件(jdbc.properties)
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///mybatis
jdbc.username=root
jdbc.password=123456
Spring配置文件(applicationContext.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--1.jdbc-properties 2.dataSource 3.SqlSessionFactory 4.mapper映射器-->
<!--扫描service层-->
<context:component-scan base-package="cn.itsource.ssm.service"/>
<!--引入jdbc.properties配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源,连接池-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!--配置SQLSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--引入数据源-->
<property name="dataSource" ref="dataSource"/>
<!--为domain取别名-->
<property name="typeAliasesPackage" value="cn.itsource.ssm.domain,cn.itsource.ssm.query"/>
<!--找我们的映射文件-->
<property name="mapperLocations" value="classpath:cn/itsource/ssm/mapper/*.xml"/>
</bean>
<!--配置注入映射器-->
<!--一劳永益的方法-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.itsource.ssm.mapper"/>
</bean>
<!--配置事务-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--注解支持-->
<tx:annotation-driven/>
</beans>
springmvc配置文件(ApplicationContext-mvc.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!--扫描controller层-->
<context:component-scan base-package="cn.itsource.ssm.web.controller"/>
<!--静态资源放行-->
<mvc:default-servlet-handler/>
<!--支持注解-->
<mvc:annotation-driven/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
WEB.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--核心控制器-->
<servlet>
<servlet-name>dispatchServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--读取SpringMVC的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:ApplicationContext-mvc.xml</param-value>
</init-param>
<!--Tomcat启动就启动-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatchServlet</servlet-name>
<!--符合RESTful风格-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--Spring监听器(读取核心文件)-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:ApplicationContext.xml</param-value>
</context-param>
<!--POST请求的中文编码过滤器-->
<!-- 字符集 过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
项目结构图

注意在创建resources文件夹下面创建文件夹时不能用cn.itsource.ssm.mapper,要用cn/itsource/ssm/mapper
例子
domain(Employee)
package cn.itsource.ssm.domain;
public class Employee {
private Long id;
private String name;
private Integer age;
private Boolean sex;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getSex() {
return sex;
}
public void setSex(Boolean sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
'}';
}
}
mapper(EmployeeMapper)
package cn.itsource.ssm.mapper;
import cn.itsource.ssm.domain.Employee;
import java.util.List;
public interface EmployeeMapper {
List<Employee> findAll();
Employee findOne(Long id);
void save(Employee employee);
void delete(Long id);
void update(Employee employee);
}
service(EmployeeSeviceImpl)
package cn.itsource.ssm.service.impl;
import cn.itsource.ssm.domain.Employee;
import cn.itsource.ssm.mapper.EmployeeMapper;
import cn.itsource.ssm.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EmployeeServiceImpl implements IEmployeeService {
@Autowired
private EmployeeMapper employeeMapper;
@Override
public List<Employee> findAll() {
return employeeMapper.findAll();
}
@Override
public Employee findOne(Long id) {
return employeeMapper.findOne(id);
}
@Override
public void save(Employee employee) {
employeeMapper.save(employee);
}
@Override
public void delete(Long id) {
employeeMapper.delete(id);
}
@Override
public void update(Employee employee) {
employeeMapper.update(employee);
}
}
controller(EmployeeController)
package cn.itsource.ssm.web.controller;
import cn.itsource.ssm.domain.Employee;
import cn.itsource.ssm.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private IEmployeeService employeeService;
@RequestMapping("index")
public String index(){
return "employee";
}
@RequestMapping("/list")
@ResponseBody
public List<Employee> findAll(){
List<Employee> list = employeeService.findAll();
return list;
}
}
映射文件(EmployeeMapper.xml)
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
这个Mapper的主要功能就是写sql
mapper:根
namespace:命令空间 (用来确定唯一) 以前这个是可以不加的,现在必需加
namespace的值,规则的:映射文件XxxMapper.xml所在的包+domain类名+Mapper
-->
<mapper namespace="cn.itsource.ssm.mapper.EmployeeMapper">
<select id="findAll" resultType="employee">
select * from employee
</select>
</mapper>
浙公网安备 33010602011771号