概述
SSM框架集成
本章目标
搭建Spring + SpringMVC + MyBatis集成环境。
搭建步骤
手动导入jar包
spring+springmvc的jar包
mybatisjar包
mybatis-spring-1.3.1.jar
c3p0的jar包
log4j的jar包
jstl的jar包
jacksonjar包
数据库的jar的包
Common-fileupload
Maven依赖
4.0.0
com.znsd.ssm
ssm-web
war
0.0.1-SNAPSHOT
ssm-web Maven Webapp
http://maven.apache.org
<distributionManagement>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Snapshots</name>
<url>http://nexus.znsd.com/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>nexus-releases</id>
<name>Releases</name>
<url>http://nexus.znsd.com/content/repositories/releases/</url>
</repository>
</distributionManagement>
<!-- 依赖属性定义 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.3.RELEASE</spring.version>
<javassist.version>3.11.0.GA</javassist.version>
<aspectj.version>1.5.3</aspectj.version>
<asm.version>3.2</asm.version>
<cglib.version>2.2.2</cglib.version>
<jackson.version>1.9.13</jackson.version>
<mybatis.version>3.2.8</mybatis.version>
<mybatis-spring.version>1.2.2</mybatis-spring.version>
<mysql-jdbc.version>5.0.8</mysql-jdbc.version>
<hessian.version>4.0.38</hessian.version>
<druid.version>1.1.10</druid.version>
<slf4j.version>1.6.1</slf4j.version>
<logback.version>1.1.2</logback.version>
<commons-codec.version>1.3</commons-codec.version>
</properties>
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring end -->
<!-- javassist -->
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javassist.version}</version>
</dependency>
<!-- aspectj -->
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!-- asm -->
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>${asm.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
</dependency>
<!-- json -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-jdbc.version}</version>
</dependency>
<!-- spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- jsp servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jsptags</groupId>
<artifactId>pager-taglib</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- alibaba 数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- log -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
<build>
<finalName>ssm-web</finalName>
<plugins>
<!-- 编译插件,默认使用jdk1.8 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 使用utf-8编码处理资源文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
添加log4j配置文件 由于MyBatis依赖与log4j输出sql语句信息,所以需要配置log4j配置文件。
#设置输出级别和输出位置
log4j.rootLogger=debug,Console
#设置控制台相关的参数
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
#设置MyBatis的输出内容
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
配置web.xml
添加SpringMVC核心控制器DispatcheerServlet配置信息
<context:component-scan base-package=“com.znsd.ssm” />
<mvc:default-servlet-handler />
<mvc:annotation-driven />
配置文件上传
配置文件上传组件。
<context:property-placeholder location=“classpath:jdbc.properties” />
jdbc.properties文件
##jdbcu914Du7F6E##
jdbc.driver=com.mysql.jdbc.Driver
jdbc.dbname=test
jdbc.port=3306
jdbc.host=192.168.41.10
jdbc.user=znsd_test
jdbc.password=123456
jdbc.pool.minPoolSize=5
jdbc.pool.maxPoolSize=30
jdbc.pool.initialPoolSize=5
jdbc.pool.maxIdleTime=60
jdbc.pool.timeBetweenEvictionRunsMillis=60000
jdbc.pool.minEvictableIdleTimeMillis=300000
配置sqlSessionFactory
MyBatis中使用sqlSessionFactory来生产session对象,和Hibernate中相似,也可以交由spring来管理。
设置扫描的映射文件和dao包
classpath*:/mybatis-mapping/*.xml Mybatis映射文件 在SSM中,可以省略mybatis配置文件,也可以添加,只不过需要在sqlSessionFactory中配置configLocation属性即可。 <?xml version="1.0" encoding="UTF-8" ?> 配置事务 在Spring里面,可以通过AOP来管理事务,非常强大。<tx:advice id=“txAdvice” transaction-manager=“txManager”>
tx:attributes
<tx:method name=“find*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“search*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“query*” propagation=“REQUIRED” read-only=“true” />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="submit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
实现查询学生列表信息
使用SSM来查询学生列表信息
获取学生列表的json格式数据
image
添加实体类
学生实体类
public class Student implements Serializable {
private Integer id;
private String name;
private String password;
private String gender;
private String address;
private Clazz clazz;
// 省略getter和setter方法
}
映射文件
StudentDao接口
@Repository
public interface StudentDao {
public List selectList();
}
映射文件StudentMapper.xml
<resultMap id="BaseResultMap" type="com.znsd.ssm.bean.Student">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="gender" property="gender" jdbcType="VARCHAR" />
<result column="c_id" property="clazz.classId" jdbcType="INTEGER" />
</resultMap>
<select id="selectList" resultMap="BaseResultMap">
select * from t_student
</select>
Service层 StudentService代码
public interface StudentService {
public List selectList();
}
StudentService实现类
@Service
public class StudentServiceImpl implements StudentService {
@Resource
private StudentDao studentDao;
@Override
public List<Student> selectList() {
return this.studentDao.selectList();
}
}
Controller层
由控制器层将数据返回对应的视图,输出json格式的数据。
@Controller
@RequestMapping(“student”)
public class StudentController {
@Autowired
private StudentService service;
@RequestMapping("/list")
public String getStudentList(Map<String, Object> map) {
List<Student> studentList = service.selectList();
map.put("studentList", studentList);
return "studentList";
}
@ResponseBody
@RequestMapping("/json")
public List<Student> getlistJson() {
List<Student> list = service.selectList();
return list;
}
}
View视图层
显示学生列表
id | name | pass | sex |
${stu.id} | ${stu.name} | ${stu.password} | ${stu.gender} |
imageimage
总结
SSM整合本章目标
搭建Spring + SpringMVC + MyBatis集成环境。
搭建步骤
手动导入jar包
spring+springmvc的jar包
mybatisjar包
mybatis-spring-1.3.1.jar
c3p0的jar包
log4j的jar包
jstl的jar包
jacksonjar包
数据库的jar的包
Common-fileupload
Maven依赖
4.0.0
com.znsd.ssm
ssm-web
war
0.0.1-SNAPSHOT
ssm-web Maven Webapp
http://maven.apache.org
<distributionManagement>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Snapshots</name>
<url>http://nexus.znsd.com/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>nexus-releases</id>
<name>Releases</name>
<url>http://nexus.znsd.com/content/repositories/releases/</url>
</repository>
</distributionManagement>
<!-- 依赖属性定义 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.3.RELEASE</spring.version>
<javassist.version>3.11.0.GA</javassist.version>
<aspectj.version>1.5.3</aspectj.version>
<asm.version>3.2</asm.version>
<cglib.version>2.2.2</cglib.version>
<jackson.version>1.9.13</jackson.version>
<mybatis.version>3.2.8</mybatis.version>
<mybatis-spring.version>1.2.2</mybatis-spring.version>
<mysql-jdbc.version>5.0.8</mysql-jdbc.version>
<hessian.version>4.0.38</hessian.version>
<druid.version>1.1.10</druid.version>
<slf4j.version>1.6.1</slf4j.version>
<logback.version>1.1.2</logback.version>
<commons-codec.version>1.3</commons-codec.version>
</properties>
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring end -->
<!-- javassist -->
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javassist.version}</version>
</dependency>
<!-- aspectj -->
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!-- asm -->
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>${asm.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
</dependency>
<!-- json -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-jdbc.version}</version>
</dependency>
<!-- spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- jsp servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jsptags</groupId>
<artifactId>pager-taglib</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- alibaba 数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- log -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
<build>
<finalName>ssm-web</finalName>
<plugins>
<!-- 编译插件,默认使用jdk1.8 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 使用utf-8编码处理资源文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
添加log4j配置文件 由于MyBatis依赖与log4j输出sql语句信息,所以需要配置log4j配置文件。
#设置输出级别和输出位置
log4j.rootLogger=debug,Console
#设置控制台相关的参数
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
#设置MyBatis的输出内容
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
配置web.xml
添加SpringMVC核心控制器DispatcheerServlet配置信息
<context:component-scan base-package=“com.znsd.ssm” />
<mvc:default-servlet-handler />
<mvc:annotation-driven />
配置文件上传
配置文件上传组件。
<context:property-placeholder location=“classpath:jdbc.properties” />
jdbc.properties文件
##jdbcu914Du7F6E##
jdbc.driver=com.mysql.jdbc.Driver
jdbc.dbname=test
jdbc.port=3306
jdbc.host=192.168.41.10
jdbc.user=znsd_test
jdbc.password=123456
jdbc.pool.minPoolSize=5
jdbc.pool.maxPoolSize=30
jdbc.pool.initialPoolSize=5
jdbc.pool.maxIdleTime=60
jdbc.pool.timeBetweenEvictionRunsMillis=60000
jdbc.pool.minEvictableIdleTimeMillis=300000
配置sqlSessionFactory
MyBatis中使用sqlSessionFactory来生产session对象,和Hibernate中相似,也可以交由spring来管理。
设置扫描的映射文件和dao包
classpath*:/mybatis-mapping/*.xml Mybatis映射文件 在SSM中,可以省略mybatis配置文件,也可以添加,只不过需要在sqlSessionFactory中配置configLocation属性即可。 <?xml version="1.0" encoding="UTF-8" ?> 配置事务 在Spring里面,可以通过AOP来管理事务,非常强大。<tx:advice id=“txAdvice” transaction-manager=“txManager”>
tx:attributes
<tx:method name=“find*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“search*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“query*” propagation=“REQUIRED” read-only=“true” />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="submit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
实现查询学生列表信息
使用SSM来查询学生列表信息
获取学生列表的json格式数据
image
添加实体类
学生实体类
public class Student implements Serializable {
private Integer id;
private String name;
private String password;
private String gender;
private String address;
private Clazz clazz;
// 省略getter和setter方法
}
映射文件
StudentDao接口
@Repository
public interface StudentDao {
public List selectList();
}
映射文件StudentMapper.xml
<resultMap id="BaseResultMap" type="com.znsd.ssm.bean.Student">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="gender" property="gender" jdbcType="VARCHAR" />
<result column="c_id" property="clazz.classId" jdbcType="INTEGER" />
</resultMap>
<select id="selectList" resultMap="BaseResultMap">
select * from t_student
</select>
Service层 StudentService代码
public interface StudentService {
public List selectList();
}
StudentService实现类
@Service
public class StudentServiceImpl implements StudentService {
@Resource
private StudentDao studentDao;
@Override
public List<Student> selectList() {
return this.studentDao.selectList();
}
}
Controller层
由控制器层将数据返回对应的视图,输出json格式的数据。
@Controller
@RequestMapping(“student”)
public class StudentController {
@Autowired
private StudentService service;
@RequestMapping("/list")
public String getStudentList(Map<String, Object> map) {
List<Student> studentList = service.selectList();
map.put("studentList", studentList);
return "studentList";
}
@ResponseBody
@RequestMapping("/json")
public List<Student> getlistJson() {
List<Student> list = service.selectList();
return list;
}
}
View视图层
显示学生列表
id | name | pass | sex |
${stu.id} | ${stu.name} | ${stu.password} | ${stu.gender} |
imageimage
总结
SSM整合本章目标
搭建Spring + SpringMVC + MyBatis集成环境。
搭建步骤
手动导入jar包
spring+springmvc的jar包
mybatisjar包
mybatis-spring-1.3.1.jar
c3p0的jar包
log4j的jar包
jstl的jar包
jacksonjar包
数据库的jar的包
Common-fileupload
Maven依赖
4.0.0
com.znsd.ssm
ssm-web
war
0.0.1-SNAPSHOT
ssm-web Maven Webapp
http://maven.apache.org
<distributionManagement>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Snapshots</name>
<url>http://nexus.znsd.com/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>nexus-releases</id>
<name>Releases</name>
<url>http://nexus.znsd.com/content/repositories/releases/</url>
</repository>
</distributionManagement>
<!-- 依赖属性定义 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.3.RELEASE</spring.version>
<javassist.version>3.11.0.GA</javassist.version>
<aspectj.version>1.5.3</aspectj.version>
<asm.version>3.2</asm.version>
<cglib.version>2.2.2</cglib.version>
<jackson.version>1.9.13</jackson.version>
<mybatis.version>3.2.8</mybatis.version>
<mybatis-spring.version>1.2.2</mybatis-spring.version>
<mysql-jdbc.version>5.0.8</mysql-jdbc.version>
<hessian.version>4.0.38</hessian.version>
<druid.version>1.1.10</druid.version>
<slf4j.version>1.6.1</slf4j.version>
<logback.version>1.1.2</logback.version>
<commons-codec.version>1.3</commons-codec.version>
</properties>
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring end -->
<!-- javassist -->
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javassist.version}</version>
</dependency>
<!-- aspectj -->
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!-- asm -->
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>${asm.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
</dependency>
<!-- json -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-jdbc.version}</version>
</dependency>
<!-- spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- jsp servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jsptags</groupId>
<artifactId>pager-taglib</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- alibaba 数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- log -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
<build>
<finalName>ssm-web</finalName>
<plugins>
<!-- 编译插件,默认使用jdk1.8 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 使用utf-8编码处理资源文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
添加log4j配置文件 由于MyBatis依赖与log4j输出sql语句信息,所以需要配置log4j配置文件。
#设置输出级别和输出位置
log4j.rootLogger=debug,Console
#设置控制台相关的参数
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
#设置MyBatis的输出内容
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
配置web.xml
添加SpringMVC核心控制器DispatcheerServlet配置信息
<context:component-scan base-package=“com.znsd.ssm” />
<mvc:default-servlet-handler />
<mvc:annotation-driven />
配置文件上传
配置文件上传组件。
<context:property-placeholder location=“classpath:jdbc.properties” />
jdbc.properties文件
##jdbcu914Du7F6E##
jdbc.driver=com.mysql.jdbc.Driver
jdbc.dbname=test
jdbc.port=3306
jdbc.host=192.168.41.10
jdbc.user=znsd_test
jdbc.password=123456
jdbc.pool.minPoolSize=5
jdbc.pool.maxPoolSize=30
jdbc.pool.initialPoolSize=5
jdbc.pool.maxIdleTime=60
jdbc.pool.timeBetweenEvictionRunsMillis=60000
jdbc.pool.minEvictableIdleTimeMillis=300000
配置sqlSessionFactory
MyBatis中使用sqlSessionFactory来生产session对象,和Hibernate中相似,也可以交由spring来管理。
设置扫描的映射文件和dao包
classpath*:/mybatis-mapping/*.xml Mybatis映射文件 在SSM中,可以省略mybatis配置文件,也可以添加,只不过需要在sqlSessionFactory中配置configLocation属性即可。 <?xml version="1.0" encoding="UTF-8" ?> 配置事务 在Spring里面,可以通过AOP来管理事务,非常强大。<tx:advice id=“txAdvice” transaction-manager=“txManager”>
tx:attributes
<tx:method name=“find*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“search*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“query*” propagation=“REQUIRED” read-only=“true” />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="submit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
实现查询学生列表信息
使用SSM来查询学生列表信息
获取学生列表的json格式数据
image
添加实体类
学生实体类
public class Student implements Serializable {
private Integer id;
private String name;
private String password;
private String gender;
private String address;
private Clazz clazz;
// 省略getter和setter方法
}
映射文件
StudentDao接口
@Repository
public interface StudentDao {
public List selectList();
}
映射文件StudentMapper.xml
<resultMap id="BaseResultMap" type="com.znsd.ssm.bean.Student">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="gender" property="gender" jdbcType="VARCHAR" />
<result column="c_id" property="clazz.classId" jdbcType="INTEGER" />
</resultMap>
<select id="selectList" resultMap="BaseResultMap">
select * from t_student
</select>
Service层 StudentService代码
public interface StudentService {
public List selectList();
}
StudentService实现类
@Service
public class StudentServiceImpl implements StudentService {
@Resource
private StudentDao studentDao;
@Override
public List<Student> selectList() {
return this.studentDao.selectList();
}
}
Controller层
由控制器层将数据返回对应的视图,输出json格式的数据。
@Controller
@RequestMapping(“student”)
public class StudentController {
@Autowired
private StudentService service;
@RequestMapping("/list")
public String getStudentList(Map<String, Object> map) {
List<Student> studentList = service.selectList();
map.put("studentList", studentList);
return "studentList";
}
@ResponseBody
@RequestMapping("/json")
public List<Student> getlistJson() {
List<Student> list = service.selectList();
return list;
}
}
View视图层
显示学生列表
id | name | pass | sex |
${stu.id} | ${stu.name} | ${stu.password} | ${stu.gender} |
imageimage
总结
SSM整合本章目标
搭建Spring + SpringMVC + MyBatis集成环境。
搭建步骤
手动导入jar包
spring+springmvc的jar包
mybatisjar包
mybatis-spring-1.3.1.jar
c3p0的jar包
log4j的jar包
jstl的jar包
jacksonjar包
数据库的jar的包
Common-fileupload
Maven依赖
4.0.0
com.znsd.ssm
ssm-web
war
0.0.1-SNAPSHOT
ssm-web Maven Webapp
http://maven.apache.org
<distributionManagement>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Snapshots</name>
<url>http://nexus.znsd.com/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>nexus-releases</id>
<name>Releases</name>
<url>http://nexus.znsd.com/content/repositories/releases/</url>
</repository>
</distributionManagement>
<!-- 依赖属性定义 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.3.RELEASE</spring.version>
<javassist.version>3.11.0.GA</javassist.version>
<aspectj.version>1.5.3</aspectj.version>
<asm.version>3.2</asm.version>
<cglib.version>2.2.2</cglib.version>
<jackson.version>1.9.13</jackson.version>
<mybatis.version>3.2.8</mybatis.version>
<mybatis-spring.version>1.2.2</mybatis-spring.version>
<mysql-jdbc.version>5.0.8</mysql-jdbc.version>
<hessian.version>4.0.38</hessian.version>
<druid.version>1.1.10</druid.version>
<slf4j.version>1.6.1</slf4j.version>
<logback.version>1.1.2</logback.version>
<commons-codec.version>1.3</commons-codec.version>
</properties>
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring end -->
<!-- javassist -->
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javassist.version}</version>
</dependency>
<!-- aspectj -->
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!-- asm -->
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>${asm.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
</dependency>
<!-- json -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-jdbc.version}</version>
</dependency>
<!-- spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- jsp servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jsptags</groupId>
<artifactId>pager-taglib</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- alibaba 数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- log -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
<build>
<finalName>ssm-web</finalName>
<plugins>
<!-- 编译插件,默认使用jdk1.8 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 使用utf-8编码处理资源文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
添加log4j配置文件 由于MyBatis依赖与log4j输出sql语句信息,所以需要配置log4j配置文件。
#设置输出级别和输出位置
log4j.rootLogger=debug,Console
#设置控制台相关的参数
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
#设置MyBatis的输出内容
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
配置web.xml
添加SpringMVC核心控制器DispatcheerServlet配置信息
<context:component-scan base-package=“com.znsd.ssm” />
<mvc:default-servlet-handler />
<mvc:annotation-driven />
配置文件上传
配置文件上传组件。
<context:property-placeholder location=“classpath:jdbc.properties” />
jdbc.properties文件
##jdbcu914Du7F6E##
jdbc.driver=com.mysql.jdbc.Driver
jdbc.dbname=test
jdbc.port=3306
jdbc.host=192.168.41.10
jdbc.user=znsd_test
jdbc.password=123456
jdbc.pool.minPoolSize=5
jdbc.pool.maxPoolSize=30
jdbc.pool.initialPoolSize=5
jdbc.pool.maxIdleTime=60
jdbc.pool.timeBetweenEvictionRunsMillis=60000
jdbc.pool.minEvictableIdleTimeMillis=300000
配置sqlSessionFactory
MyBatis中使用sqlSessionFactory来生产session对象,和Hibernate中相似,也可以交由spring来管理。
设置扫描的映射文件和dao包
classpath*:/mybatis-mapping/*.xml Mybatis映射文件 在SSM中,可以省略mybatis配置文件,也可以添加,只不过需要在sqlSessionFactory中配置configLocation属性即可。 <?xml version="1.0" encoding="UTF-8" ?> 配置事务 在Spring里面,可以通过AOP来管理事务,非常强大。<tx:advice id=“txAdvice” transaction-manager=“txManager”>
tx:attributes
<tx:method name=“find*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“search*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“query*” propagation=“REQUIRED” read-only=“true” />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="submit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
实现查询学生列表信息
使用SSM来查询学生列表信息
获取学生列表的json格式数据
image
添加实体类
学生实体类
public class Student implements Serializable {
private Integer id;
private String name;
private String password;
private String gender;
private String address;
private Clazz clazz;
// 省略getter和setter方法
}
映射文件
StudentDao接口
@Repository
public interface StudentDao {
public List selectList();
}
映射文件StudentMapper.xml
<resultMap id="BaseResultMap" type="com.znsd.ssm.bean.Student">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="gender" property="gender" jdbcType="VARCHAR" />
<result column="c_id" property="clazz.classId" jdbcType="INTEGER" />
</resultMap>
<select id="selectList" resultMap="BaseResultMap">
select * from t_student
</select>
Service层 StudentService代码
public interface StudentService {
public List selectList();
}
StudentService实现类
@Service
public class StudentServiceImpl implements StudentService {
@Resource
private StudentDao studentDao;
@Override
public List<Student> selectList() {
return this.studentDao.selectList();
}
}
Controller层
由控制器层将数据返回对应的视图,输出json格式的数据。
@Controller
@RequestMapping(“student”)
public class StudentController {
@Autowired
private StudentService service;
@RequestMapping("/list")
public String getStudentList(Map<String, Object> map) {
List<Student> studentList = service.selectList();
map.put("studentList", studentList);
return "studentList";
}
@ResponseBody
@RequestMapping("/json")
public List<Student> getlistJson() {
List<Student> list = service.selectList();
return list;
}
}
View视图层
显示学生列表
id | name | pass | sex |
${stu.id} | ${stu.name} | ${stu.password} | ${stu.gender} |
imageimage
总结
SSM整合本章目标
搭建Spring + SpringMVC + MyBatis集成环境。
搭建步骤
手动导入jar包
spring+springmvc的jar包
mybatisjar包
mybatis-spring-1.3.1.jar
c3p0的jar包
log4j的jar包
jstl的jar包
jacksonjar包
数据库的jar的包
Common-fileupload
Maven依赖
4.0.0
com.znsd.ssm
ssm-web
war
0.0.1-SNAPSHOT
ssm-web Maven Webapp
http://maven.apache.org
<distributionManagement>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Snapshots</name>
<url>http://nexus.znsd.com/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>nexus-releases</id>
<name>Releases</name>
<url>http://nexus.znsd.com/content/repositories/releases/</url>
</repository>
</distributionManagement>
<!-- 依赖属性定义 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.3.RELEASE</spring.version>
<javassist.version>3.11.0.GA</javassist.version>
<aspectj.version>1.5.3</aspectj.version>
<asm.version>3.2</asm.version>
<cglib.version>2.2.2</cglib.version>
<jackson.version>1.9.13</jackson.version>
<mybatis.version>3.2.8</mybatis.version>
<mybatis-spring.version>1.2.2</mybatis-spring.version>
<mysql-jdbc.version>5.0.8</mysql-jdbc.version>
<hessian.version>4.0.38</hessian.version>
<druid.version>1.1.10</druid.version>
<slf4j.version>1.6.1</slf4j.version>
<logback.version>1.1.2</logback.version>
<commons-codec.version>1.3</commons-codec.version>
</properties>
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring end -->
<!-- javassist -->
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javassist.version}</version>
</dependency>
<!-- aspectj -->
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!-- asm -->
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>${asm.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
</dependency>
<!-- json -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-jdbc.version}</version>
</dependency>
<!-- spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- jsp servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jsptags</groupId>
<artifactId>pager-taglib</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- alibaba 数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- log -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
<build>
<finalName>ssm-web</finalName>
<plugins>
<!-- 编译插件,默认使用jdk1.8 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 使用utf-8编码处理资源文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
添加log4j配置文件 由于MyBatis依赖与log4j输出sql语句信息,所以需要配置log4j配置文件。
#设置输出级别和输出位置
log4j.rootLogger=debug,Console
#设置控制台相关的参数
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
#设置MyBatis的输出内容
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
配置web.xml
添加SpringMVC核心控制器DispatcheerServlet配置信息
<context:component-scan base-package=“com.znsd.ssm” />
<mvc:default-servlet-handler />
<mvc:annotation-driven />
配置文件上传
配置文件上传组件。
<context:property-placeholder location=“classpath:jdbc.properties” />
jdbc.properties文件
##jdbcu914Du7F6E##
jdbc.driver=com.mysql.jdbc.Driver
jdbc.dbname=test
jdbc.port=3306
jdbc.host=192.168.41.10
jdbc.user=znsd_test
jdbc.password=123456
jdbc.pool.minPoolSize=5
jdbc.pool.maxPoolSize=30
jdbc.pool.initialPoolSize=5
jdbc.pool.maxIdleTime=60
jdbc.pool.timeBetweenEvictionRunsMillis=60000
jdbc.pool.minEvictableIdleTimeMillis=300000
配置sqlSessionFactory
MyBatis中使用sqlSessionFactory来生产session对象,和Hibernate中相似,也可以交由spring来管理。
设置扫描的映射文件和dao包
classpath*:/mybatis-mapping/*.xml Mybatis映射文件 在SSM中,可以省略mybatis配置文件,也可以添加,只不过需要在sqlSessionFactory中配置configLocation属性即可。 <?xml version="1.0" encoding="UTF-8" ?> 配置事务 在Spring里面,可以通过AOP来管理事务,非常强大。<tx:advice id=“txAdvice” transaction-manager=“txManager”>
tx:attributes
<tx:method name=“find*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“search*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“query*” propagation=“REQUIRED” read-only=“true” />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="submit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
实现查询学生列表信息
使用SSM来查询学生列表信息
获取学生列表的json格式数据
image
添加实体类
学生实体类
public class Student implements Serializable {
private Integer id;
private String name;
private String password;
private String gender;
private String address;
private Clazz clazz;
// 省略getter和setter方法
}
映射文件
StudentDao接口
@Repository
public interface StudentDao {
public List selectList();
}
映射文件StudentMapper.xml
<resultMap id="BaseResultMap" type="com.znsd.ssm.bean.Student">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="gender" property="gender" jdbcType="VARCHAR" />
<result column="c_id" property="clazz.classId" jdbcType="INTEGER" />
</resultMap>
<select id="selectList" resultMap="BaseResultMap">
select * from t_student
</select>
Service层 StudentService代码
public interface StudentService {
public List selectList();
}
StudentService实现类
@Service
public class StudentServiceImpl implements StudentService {
@Resource
private StudentDao studentDao;
@Override
public List<Student> selectList() {
return this.studentDao.selectList();
}
}
Controller层
由控制器层将数据返回对应的视图,输出json格式的数据。
@Controller
@RequestMapping(“student”)
public class StudentController {
@Autowired
private StudentService service;
@RequestMapping("/list")
public String getStudentList(Map<String, Object> map) {
List<Student> studentList = service.selectList();
map.put("studentList", studentList);
return "studentList";
}
@ResponseBody
@RequestMapping("/json")
public List<Student> getlistJson() {
List<Student> list = service.selectList();
return list;
}
}
View视图层
显示学生列表
id | name | pass | sex |
${stu.id} | ${stu.name} | ${stu.password} | ${stu.gender} |
imageimage
总结
SSM整合本章目标
搭建Spring + SpringMVC + MyBatis集成环境。
搭建步骤
手动导入jar包
spring+springmvc的jar包
mybatisjar包
mybatis-spring-1.3.1.jar
c3p0的jar包
log4j的jar包
jstl的jar包
jacksonjar包
数据库的jar的包
Common-fileupload
Maven依赖
4.0.0
com.znsd.ssm
ssm-web
war
0.0.1-SNAPSHOT
ssm-web Maven Webapp
http://maven.apache.org
<distributionManagement>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Snapshots</name>
<url>http://nexus.znsd.com/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>nexus-releases</id>
<name>Releases</name>
<url>http://nexus.znsd.com/content/repositories/releases/</url>
</repository>
</distributionManagement>
<!-- 依赖属性定义 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.3.RELEASE</spring.version>
<javassist.version>3.11.0.GA</javassist.version>
<aspectj.version>1.5.3</aspectj.version>
<asm.version>3.2</asm.version>
<cglib.version>2.2.2</cglib.version>
<jackson.version>1.9.13</jackson.version>
<mybatis.version>3.2.8</mybatis.version>
<mybatis-spring.version>1.2.2</mybatis-spring.version>
<mysql-jdbc.version>5.0.8</mysql-jdbc.version>
<hessian.version>4.0.38</hessian.version>
<druid.version>1.1.10</druid.version>
<slf4j.version>1.6.1</slf4j.version>
<logback.version>1.1.2</logback.version>
<commons-codec.version>1.3</commons-codec.version>
</properties>
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring end -->
<!-- javassist -->
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javassist.version}</version>
</dependency>
<!-- aspectj -->
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!-- asm -->
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>${asm.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
</dependency>
<!-- json -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-jdbc.version}</version>
</dependency>
<!-- spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- jsp servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jsptags</groupId>
<artifactId>pager-taglib</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- alibaba 数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- log -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
<build>
<finalName>ssm-web</finalName>
<plugins>
<!-- 编译插件,默认使用jdk1.8 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 使用utf-8编码处理资源文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
添加log4j配置文件 由于MyBatis依赖与log4j输出sql语句信息,所以需要配置log4j配置文件。
#设置输出级别和输出位置
log4j.rootLogger=debug,Console
#设置控制台相关的参数
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
#设置MyBatis的输出内容
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
配置web.xml
添加SpringMVC核心控制器DispatcheerServlet配置信息
<context:component-scan base-package=“com.znsd.ssm” />
<mvc:default-servlet-handler />
<mvc:annotation-driven />
配置文件上传
配置文件上传组件。
<context:property-placeholder location=“classpath:jdbc.properties” />
jdbc.properties文件
##jdbcu914Du7F6E##
jdbc.driver=com.mysql.jdbc.Driver
jdbc.dbname=test
jdbc.port=3306
jdbc.host=192.168.41.10
jdbc.user=znsd_test
jdbc.password=123456
jdbc.pool.minPoolSize=5
jdbc.pool.maxPoolSize=30
jdbc.pool.initialPoolSize=5
jdbc.pool.maxIdleTime=60
jdbc.pool.timeBetweenEvictionRunsMillis=60000
jdbc.pool.minEvictableIdleTimeMillis=300000
配置sqlSessionFactory
MyBatis中使用sqlSessionFactory来生产session对象,和Hibernate中相似,也可以交由spring来管理。
设置扫描的映射文件和dao包
classpath*:/mybatis-mapping/*.xml Mybatis映射文件 在SSM中,可以省略mybatis配置文件,也可以添加,只不过需要在sqlSessionFactory中配置configLocation属性即可。 <?xml version="1.0" encoding="UTF-8" ?> 配置事务 在Spring里面,可以通过AOP来管理事务,非常强大。<tx:advice id=“txAdvice” transaction-manager=“txManager”>
tx:attributes
<tx:method name=“find*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“search*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“query*” propagation=“REQUIRED” read-only=“true” />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="submit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
实现查询学生列表信息
使用SSM来查询学生列表信息
获取学生列表的json格式数据
image
添加实体类
学生实体类
public class Student implements Serializable {
private Integer id;
private String name;
private String password;
private String gender;
private String address;
private Clazz clazz;
// 省略getter和setter方法
}
映射文件
StudentDao接口
@Repository
public interface StudentDao {
public List selectList();
}
映射文件StudentMapper.xml
<resultMap id="BaseResultMap" type="com.znsd.ssm.bean.Student">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="gender" property="gender" jdbcType="VARCHAR" />
<result column="c_id" property="clazz.classId" jdbcType="INTEGER" />
</resultMap>
<select id="selectList" resultMap="BaseResultMap">
select * from t_student
</select>
Service层 StudentService代码
public interface StudentService {
public List selectList();
}
StudentService实现类
@Service
public class StudentServiceImpl implements StudentService {
@Resource
private StudentDao studentDao;
@Override
public List<Student> selectList() {
return this.studentDao.selectList();
}
}
Controller层
由控制器层将数据返回对应的视图,输出json格式的数据。
@Controller
@RequestMapping(“student”)
public class StudentController {
@Autowired
private StudentService service;
@RequestMapping("/list")
public String getStudentList(Map<String, Object> map) {
List<Student> studentList = service.selectList();
map.put("studentList", studentList);
return "studentList";
}
@ResponseBody
@RequestMapping("/json")
public List<Student> getlistJson() {
List<Student> list = service.selectList();
return list;
}
}
View视图层
显示学生列表
id | name | pass | sex |
${stu.id} | ${stu.name} | ${stu.password} | ${stu.gender} |
imageimage
总结
SSM整合本章目标
搭建Spring + SpringMVC + MyBatis集成环境。
搭建步骤
手动导入jar包
spring+springmvc的jar包
mybatisjar包
mybatis-spring-1.3.1.jar
c3p0的jar包
log4j的jar包
jstl的jar包
jacksonjar包
数据库的jar的包
Common-fileupload
Maven依赖
4.0.0
com.znsd.ssm
ssm-web
war
0.0.1-SNAPSHOT
ssm-web Maven Webapp
http://maven.apache.org
<distributionManagement>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Snapshots</name>
<url>http://nexus.znsd.com/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>nexus-releases</id>
<name>Releases</name>
<url>http://nexus.znsd.com/content/repositories/releases/</url>
</repository>
</distributionManagement>
<!-- 依赖属性定义 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.3.RELEASE</spring.version>
<javassist.version>3.11.0.GA</javassist.version>
<aspectj.version>1.5.3</aspectj.version>
<asm.version>3.2</asm.version>
<cglib.version>2.2.2</cglib.version>
<jackson.version>1.9.13</jackson.version>
<mybatis.version>3.2.8</mybatis.version>
<mybatis-spring.version>1.2.2</mybatis-spring.version>
<mysql-jdbc.version>5.0.8</mysql-jdbc.version>
<hessian.version>4.0.38</hessian.version>
<druid.version>1.1.10</druid.version>
<slf4j.version>1.6.1</slf4j.version>
<logback.version>1.1.2</logback.version>
<commons-codec.version>1.3</commons-codec.version>
</properties>
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring end -->
<!-- javassist -->
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javassist.version}</version>
</dependency>
<!-- aspectj -->
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!-- asm -->
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>${asm.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
</dependency>
<!-- json -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-jdbc.version}</version>
</dependency>
<!-- spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- jsp servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jsptags</groupId>
<artifactId>pager-taglib</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- alibaba 数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- log -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
<build>
<finalName>ssm-web</finalName>
<plugins>
<!-- 编译插件,默认使用jdk1.8 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 使用utf-8编码处理资源文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
添加log4j配置文件 由于MyBatis依赖与log4j输出sql语句信息,所以需要配置log4j配置文件。
#设置输出级别和输出位置
log4j.rootLogger=debug,Console
#设置控制台相关的参数
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
#设置MyBatis的输出内容
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
配置web.xml
添加SpringMVC核心控制器DispatcheerServlet配置信息
<context:component-scan base-package=“com.znsd.ssm” />
<mvc:default-servlet-handler />
<mvc:annotation-driven />
配置文件上传
配置文件上传组件。
<context:property-placeholder location=“classpath:jdbc.properties” />
jdbc.properties文件
##jdbcu914Du7F6E##
jdbc.driver=com.mysql.jdbc.Driver
jdbc.dbname=test
jdbc.port=3306
jdbc.host=192.168.41.10
jdbc.user=znsd_test
jdbc.password=123456
jdbc.pool.minPoolSize=5
jdbc.pool.maxPoolSize=30
jdbc.pool.initialPoolSize=5
jdbc.pool.maxIdleTime=60
jdbc.pool.timeBetweenEvictionRunsMillis=60000
jdbc.pool.minEvictableIdleTimeMillis=300000
配置sqlSessionFactory
MyBatis中使用sqlSessionFactory来生产session对象,和Hibernate中相似,也可以交由spring来管理。
设置扫描的映射文件和dao包
classpath*:/mybatis-mapping/*.xml Mybatis映射文件 在SSM中,可以省略mybatis配置文件,也可以添加,只不过需要在sqlSessionFactory中配置configLocation属性即可。 <?xml version="1.0" encoding="UTF-8" ?> 配置事务 在Spring里面,可以通过AOP来管理事务,非常强大。<tx:advice id=“txAdvice” transaction-manager=“txManager”>
tx:attributes
<tx:method name=“find*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“search*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“query*” propagation=“REQUIRED” read-only=“true” />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="submit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
实现查询学生列表信息
使用SSM来查询学生列表信息
获取学生列表的json格式数据
image
添加实体类
学生实体类
public class Student implements Serializable {
private Integer id;
private String name;
private String password;
private String gender;
private String address;
private Clazz clazz;
// 省略getter和setter方法
}
映射文件
StudentDao接口
@Repository
public interface StudentDao {
public List selectList();
}
映射文件StudentMapper.xml
<resultMap id="BaseResultMap" type="com.znsd.ssm.bean.Student">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="gender" property="gender" jdbcType="VARCHAR" />
<result column="c_id" property="clazz.classId" jdbcType="INTEGER" />
</resultMap>
<select id="selectList" resultMap="BaseResultMap">
select * from t_student
</select>
Service层 StudentService代码
public interface StudentService {
public List selectList();
}
StudentService实现类
@Service
public class StudentServiceImpl implements StudentService {
@Resource
private StudentDao studentDao;
@Override
public List<Student> selectList() {
return this.studentDao.selectList();
}
}
Controller层
由控制器层将数据返回对应的视图,输出json格式的数据。
@Controller
@RequestMapping(“student”)
public class StudentController {
@Autowired
private StudentService service;
@RequestMapping("/list")
public String getStudentList(Map<String, Object> map) {
List<Student> studentList = service.selectList();
map.put("studentList", studentList);
return "studentList";
}
@ResponseBody
@RequestMapping("/json")
public List<Student> getlistJson() {
List<Student> list = service.selectList();
return list;
}
}
View视图层
显示学生列表
id | name | pass | sex |
${stu.id} | ${stu.name} | ${stu.password} | ${stu.gender} |
imageimage
总结
SSM整合本章目标
搭建Spring + SpringMVC + MyBatis集成环境。
搭建步骤
手动导入jar包
spring+springmvc的jar包
mybatisjar包
mybatis-spring-1.3.1.jar
c3p0的jar包
log4j的jar包
jstl的jar包
jacksonjar包
数据库的jar的包
Common-fileupload
Maven依赖
4.0.0
com.znsd.ssm
ssm-web
war
0.0.1-SNAPSHOT
ssm-web Maven Webapp
http://maven.apache.org
<distributionManagement>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Snapshots</name>
<url>http://nexus.znsd.com/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>nexus-releases</id>
<name>Releases</name>
<url>http://nexus.znsd.com/content/repositories/releases/</url>
</repository>
</distributionManagement>
<!-- 依赖属性定义 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.3.RELEASE</spring.version>
<javassist.version>3.11.0.GA</javassist.version>
<aspectj.version>1.5.3</aspectj.version>
<asm.version>3.2</asm.version>
<cglib.version>2.2.2</cglib.version>
<jackson.version>1.9.13</jackson.version>
<mybatis.version>3.2.8</mybatis.version>
<mybatis-spring.version>1.2.2</mybatis-spring.version>
<mysql-jdbc.version>5.0.8</mysql-jdbc.version>
<hessian.version>4.0.38</hessian.version>
<druid.version>1.1.10</druid.version>
<slf4j.version>1.6.1</slf4j.version>
<logback.version>1.1.2</logback.version>
<commons-codec.version>1.3</commons-codec.version>
</properties>
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring end -->
<!-- javassist -->
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javassist.version}</version>
</dependency>
<!-- aspectj -->
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!-- asm -->
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>${asm.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
</dependency>
<!-- json -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-jdbc.version}</version>
</dependency>
<!-- spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- jsp servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jsptags</groupId>
<artifactId>pager-taglib</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- alibaba 数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- log -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
<build>
<finalName>ssm-web</finalName>
<plugins>
<!-- 编译插件,默认使用jdk1.8 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 使用utf-8编码处理资源文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
添加log4j配置文件 由于MyBatis依赖与log4j输出sql语句信息,所以需要配置log4j配置文件。
#设置输出级别和输出位置
log4j.rootLogger=debug,Console
#设置控制台相关的参数
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
#设置MyBatis的输出内容
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
配置web.xml
添加SpringMVC核心控制器DispatcheerServlet配置信息
<context:component-scan base-package=“com.znsd.ssm” />
<mvc:default-servlet-handler />
<mvc:annotation-driven />
配置文件上传
配置文件上传组件。
<context:property-placeholder location=“classpath:jdbc.properties” />
jdbc.properties文件
##jdbcu914Du7F6E##
jdbc.driver=com.mysql.jdbc.Driver
jdbc.dbname=test
jdbc.port=3306
jdbc.host=192.168.41.10
jdbc.user=znsd_test
jdbc.password=123456
jdbc.pool.minPoolSize=5
jdbc.pool.maxPoolSize=30
jdbc.pool.initialPoolSize=5
jdbc.pool.maxIdleTime=60
jdbc.pool.timeBetweenEvictionRunsMillis=60000
jdbc.pool.minEvictableIdleTimeMillis=300000
配置sqlSessionFactory
MyBatis中使用sqlSessionFactory来生产session对象,和Hibernate中相似,也可以交由spring来管理。
设置扫描的映射文件和dao包
classpath*:/mybatis-mapping/*.xml Mybatis映射文件 在SSM中,可以省略mybatis配置文件,也可以添加,只不过需要在sqlSessionFactory中配置configLocation属性即可。 <?xml version="1.0" encoding="UTF-8" ?> 配置事务 在Spring里面,可以通过AOP来管理事务,非常强大。<tx:advice id=“txAdvice” transaction-manager=“txManager”>
tx:attributes
<tx:method name=“find*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“search*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“query*” propagation=“REQUIRED” read-only=“true” />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="submit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
实现查询学生列表信息
使用SSM来查询学生列表信息
获取学生列表的json格式数据
image
添加实体类
学生实体类
public class Student implements Serializable {
private Integer id;
private String name;
private String password;
private String gender;
private String address;
private Clazz clazz;
// 省略getter和setter方法
}
映射文件
StudentDao接口
@Repository
public interface StudentDao {
public List selectList();
}
映射文件StudentMapper.xml
<resultMap id="BaseResultMap" type="com.znsd.ssm.bean.Student">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="gender" property="gender" jdbcType="VARCHAR" />
<result column="c_id" property="clazz.classId" jdbcType="INTEGER" />
</resultMap>
<select id="selectList" resultMap="BaseResultMap">
select * from t_student
</select>
Service层 StudentService代码
public interface StudentService {
public List selectList();
}
StudentService实现类
@Service
public class StudentServiceImpl implements StudentService {
@Resource
private StudentDao studentDao;
@Override
public List<Student> selectList() {
return this.studentDao.selectList();
}
}
Controller层
由控制器层将数据返回对应的视图,输出json格式的数据。
@Controller
@RequestMapping(“student”)
public class StudentController {
@Autowired
private StudentService service;
@RequestMapping("/list")
public String getStudentList(Map<String, Object> map) {
List<Student> studentList = service.selectList();
map.put("studentList", studentList);
return "studentList";
}
@ResponseBody
@RequestMapping("/json")
public List<Student> getlistJson() {
List<Student> list = service.selectList();
return list;
}
}
View视图层
显示学生列表
id | name | pass | sex |
${stu.id} | ${stu.name} | ${stu.password} | ${stu.gender} |
imageimage
总结
SSM整合本章目标
搭建Spring + SpringMVC + MyBatis集成环境。
搭建步骤
手动导入jar包
spring+springmvc的jar包
mybatisjar包
mybatis-spring-1.3.1.jar
c3p0的jar包
log4j的jar包
jstl的jar包
jacksonjar包
数据库的jar的包
Common-fileupload
Maven依赖
4.0.0
com.znsd.ssm
ssm-web
war
0.0.1-SNAPSHOT
ssm-web Maven Webapp
http://maven.apache.org
<distributionManagement>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Snapshots</name>
<url>http://nexus.znsd.com/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>nexus-releases</id>
<name>Releases</name>
<url>http://nexus.znsd.com/content/repositories/releases/</url>
</repository>
</distributionManagement>
<!-- 依赖属性定义 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.3.RELEASE</spring.version>
<javassist.version>3.11.0.GA</javassist.version>
<aspectj.version>1.5.3</aspectj.version>
<asm.version>3.2</asm.version>
<cglib.version>2.2.2</cglib.version>
<jackson.version>1.9.13</jackson.version>
<mybatis.version>3.2.8</mybatis.version>
<mybatis-spring.version>1.2.2</mybatis-spring.version>
<mysql-jdbc.version>5.0.8</mysql-jdbc.version>
<hessian.version>4.0.38</hessian.version>
<druid.version>1.1.10</druid.version>
<slf4j.version>1.6.1</slf4j.version>
<logback.version>1.1.2</logback.version>
<commons-codec.version>1.3</commons-codec.version>
</properties>
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring end -->
<!-- javassist -->
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javassist.version}</version>
</dependency>
<!-- aspectj -->
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!-- asm -->
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>${asm.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
</dependency>
<!-- json -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-jdbc.version}</version>
</dependency>
<!-- spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- jsp servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jsptags</groupId>
<artifactId>pager-taglib</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- alibaba 数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- log -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
<build>
<finalName>ssm-web</finalName>
<plugins>
<!-- 编译插件,默认使用jdk1.8 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 使用utf-8编码处理资源文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
添加log4j配置文件 由于MyBatis依赖与log4j输出sql语句信息,所以需要配置log4j配置文件。
#设置输出级别和输出位置
log4j.rootLogger=debug,Console
#设置控制台相关的参数
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
#设置MyBatis的输出内容
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
配置web.xml
添加SpringMVC核心控制器DispatcheerServlet配置信息
<context:component-scan base-package=“com.znsd.ssm” />
<mvc:default-servlet-handler />
<mvc:annotation-driven />
配置文件上传
配置文件上传组件。
<context:property-placeholder location=“classpath:jdbc.properties” />
jdbc.properties文件
##jdbcu914Du7F6E##
jdbc.driver=com.mysql.jdbc.Driver
jdbc.dbname=test
jdbc.port=3306
jdbc.host=192.168.41.10
jdbc.user=znsd_test
jdbc.password=123456
jdbc.pool.minPoolSize=5
jdbc.pool.maxPoolSize=30
jdbc.pool.initialPoolSize=5
jdbc.pool.maxIdleTime=60
jdbc.pool.timeBetweenEvictionRunsMillis=60000
jdbc.pool.minEvictableIdleTimeMillis=300000
配置sqlSessionFactory
MyBatis中使用sqlSessionFactory来生产session对象,和Hibernate中相似,也可以交由spring来管理。
设置扫描的映射文件和dao包
classpath*:/mybatis-mapping/*.xml Mybatis映射文件 在SSM中,可以省略mybatis配置文件,也可以添加,只不过需要在sqlSessionFactory中配置configLocation属性即可。 <?xml version="1.0" encoding="UTF-8" ?> 配置事务 在Spring里面,可以通过AOP来管理事务,非常强大。<tx:advice id=“txAdvice” transaction-manager=“txManager”>
tx:attributes
<tx:method name=“find*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“search*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“query*” propagation=“REQUIRED” read-only=“true” />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="submit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
实现查询学生列表信息
使用SSM来查询学生列表信息
获取学生列表的json格式数据
image
添加实体类
学生实体类
public class Student implements Serializable {
private Integer id;
private String name;
private String password;
private String gender;
private String address;
private Clazz clazz;
// 省略getter和setter方法
}
映射文件
StudentDao接口
@Repository
public interface StudentDao {
public List selectList();
}
映射文件StudentMapper.xml
<resultMap id="BaseResultMap" type="com.znsd.ssm.bean.Student">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="gender" property="gender" jdbcType="VARCHAR" />
<result column="c_id" property="clazz.classId" jdbcType="INTEGER" />
</resultMap>
<select id="selectList" resultMap="BaseResultMap">
select * from t_student
</select>
Service层 StudentService代码
public interface StudentService {
public List selectList();
}
StudentService实现类
@Service
public class StudentServiceImpl implements StudentService {
@Resource
private StudentDao studentDao;
@Override
public List<Student> selectList() {
return this.studentDao.selectList();
}
}
Controller层
由控制器层将数据返回对应的视图,输出json格式的数据。
@Controller
@RequestMapping(“student”)
public class StudentController {
@Autowired
private StudentService service;
@RequestMapping("/list")
public String getStudentList(Map<String, Object> map) {
List<Student> studentList = service.selectList();
map.put("studentList", studentList);
return "studentList";
}
@ResponseBody
@RequestMapping("/json")
public List<Student> getlistJson() {
List<Student> list = service.selectList();
return list;
}
}
View视图层
显示学生列表
id | name | pass | sex |
${stu.id} | ${stu.name} | ${stu.password} | ${stu.gender} |
imageimage
总结
SSM整合本章目标
搭建Spring + SpringMVC + MyBatis集成环境。
搭建步骤
手动导入jar包
spring+springmvc的jar包
mybatisjar包
mybatis-spring-1.3.1.jar
c3p0的jar包
log4j的jar包
jstl的jar包
jacksonjar包
数据库的jar的包
Common-fileupload
Maven依赖
4.0.0
com.znsd.ssm
ssm-web
war
0.0.1-SNAPSHOT
ssm-web Maven Webapp
http://maven.apache.org
<distributionManagement>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Snapshots</name>
<url>http://nexus.znsd.com/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>nexus-releases</id>
<name>Releases</name>
<url>http://nexus.znsd.com/content/repositories/releases/</url>
</repository>
</distributionManagement>
<!-- 依赖属性定义 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.3.RELEASE</spring.version>
<javassist.version>3.11.0.GA</javassist.version>
<aspectj.version>1.5.3</aspectj.version>
<asm.version>3.2</asm.version>
<cglib.version>2.2.2</cglib.version>
<jackson.version>1.9.13</jackson.version>
<mybatis.version>3.2.8</mybatis.version>
<mybatis-spring.version>1.2.2</mybatis-spring.version>
<mysql-jdbc.version>5.0.8</mysql-jdbc.version>
<hessian.version>4.0.38</hessian.version>
<druid.version>1.1.10</druid.version>
<slf4j.version>1.6.1</slf4j.version>
<logback.version>1.1.2</logback.version>
<commons-codec.version>1.3</commons-codec.version>
</properties>
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring end -->
<!-- javassist -->
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javassist.version}</version>
</dependency>
<!-- aspectj -->
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!-- asm -->
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>${asm.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
</dependency>
<!-- json -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-jdbc.version}</version>
</dependency>
<!-- spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- jsp servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jsptags</groupId>
<artifactId>pager-taglib</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- alibaba 数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- log -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
<build>
<finalName>ssm-web</finalName>
<plugins>
<!-- 编译插件,默认使用jdk1.8 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 使用utf-8编码处理资源文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
添加log4j配置文件 由于MyBatis依赖与log4j输出sql语句信息,所以需要配置log4j配置文件。
#设置输出级别和输出位置
log4j.rootLogger=debug,Console
#设置控制台相关的参数
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
#设置MyBatis的输出内容
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
配置web.xml
添加SpringMVC核心控制器DispatcheerServlet配置信息
<context:component-scan base-package=“com.znsd.ssm” />
<mvc:default-servlet-handler />
<mvc:annotation-driven />
配置文件上传
配置文件上传组件。
<context:property-placeholder location=“classpath:jdbc.properties” />
jdbc.properties文件
##jdbcu914Du7F6E##
jdbc.driver=com.mysql.jdbc.Driver
jdbc.dbname=test
jdbc.port=3306
jdbc.host=192.168.41.10
jdbc.user=znsd_test
jdbc.password=123456
jdbc.pool.minPoolSize=5
jdbc.pool.maxPoolSize=30
jdbc.pool.initialPoolSize=5
jdbc.pool.maxIdleTime=60
jdbc.pool.timeBetweenEvictionRunsMillis=60000
jdbc.pool.minEvictableIdleTimeMillis=300000
配置sqlSessionFactory
MyBatis中使用sqlSessionFactory来生产session对象,和Hibernate中相似,也可以交由spring来管理。
设置扫描的映射文件和dao包
classpath*:/mybatis-mapping/*.xml Mybatis映射文件 在SSM中,可以省略mybatis配置文件,也可以添加,只不过需要在sqlSessionFactory中配置configLocation属性即可。 <?xml version="1.0" encoding="UTF-8" ?> 配置事务 在Spring里面,可以通过AOP来管理事务,非常强大。<tx:advice id=“txAdvice” transaction-manager=“txManager”>
tx:attributes
<tx:method name=“find*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“search*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“query*” propagation=“REQUIRED” read-only=“true” />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="submit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
实现查询学生列表信息
使用SSM来查询学生列表信息
获取学生列表的json格式数据
image
添加实体类
学生实体类
public class Student implements Serializable {
private Integer id;
private String name;
private String password;
private String gender;
private String address;
private Clazz clazz;
// 省略getter和setter方法
}
映射文件
StudentDao接口
@Repository
public interface StudentDao {
public List selectList();
}
映射文件StudentMapper.xml
<resultMap id="BaseResultMap" type="com.znsd.ssm.bean.Student">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="gender" property="gender" jdbcType="VARCHAR" />
<result column="c_id" property="clazz.classId" jdbcType="INTEGER" />
</resultMap>
<select id="selectList" resultMap="BaseResultMap">
select * from t_student
</select>
Service层 StudentService代码
public interface StudentService {
public List selectList();
}
StudentService实现类
@Service
public class StudentServiceImpl implements StudentService {
@Resource
private StudentDao studentDao;
@Override
public List<Student> selectList() {
return this.studentDao.selectList();
}
}
Controller层
由控制器层将数据返回对应的视图,输出json格式的数据。
@Controller
@RequestMapping(“student”)
public class StudentController {
@Autowired
private StudentService service;
@RequestMapping("/list")
public String getStudentList(Map<String, Object> map) {
List<Student> studentList = service.selectList();
map.put("studentList", studentList);
return "studentList";
}
@ResponseBody
@RequestMapping("/json")
public List<Student> getlistJson() {
List<Student> list = service.selectList();
return list;
}
}
View视图层
显示学生列表
id | name | pass | sex |
${stu.id} | ${stu.name} | ${stu.password} | ${stu.gender} |
imageimage
总结
SSM整合本章目标
搭建Spring + SpringMVC + MyBatis集成环境。
搭建步骤
手动导入jar包
spring+springmvc的jar包
mybatisjar包
mybatis-spring-1.3.1.jar
c3p0的jar包
log4j的jar包
jstl的jar包
jacksonjar包
数据库的jar的包
Common-fileupload
Maven依赖
4.0.0
com.znsd.ssm
ssm-web
war
0.0.1-SNAPSHOT
ssm-web Maven Webapp
http://maven.apache.org
<distributionManagement>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Snapshots</name>
<url>http://nexus.znsd.com/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>nexus-releases</id>
<name>Releases</name>
<url>http://nexus.znsd.com/content/repositories/releases/</url>
</repository>
</distributionManagement>
<!-- 依赖属性定义 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.3.RELEASE</spring.version>
<javassist.version>3.11.0.GA</javassist.version>
<aspectj.version>1.5.3</aspectj.version>
<asm.version>3.2</asm.version>
<cglib.version>2.2.2</cglib.version>
<jackson.version>1.9.13</jackson.version>
<mybatis.version>3.2.8</mybatis.version>
<mybatis-spring.version>1.2.2</mybatis-spring.version>
<mysql-jdbc.version>5.0.8</mysql-jdbc.version>
<hessian.version>4.0.38</hessian.version>
<druid.version>1.1.10</druid.version>
<slf4j.version>1.6.1</slf4j.version>
<logback.version>1.1.2</logback.version>
<commons-codec.version>1.3</commons-codec.version>
</properties>
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring end -->
<!-- javassist -->
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javassist.version}</version>
</dependency>
<!-- aspectj -->
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!-- asm -->
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>${asm.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
</dependency>
<!-- json -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-jdbc.version}</version>
</dependency>
<!-- spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- jsp servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jsptags</groupId>
<artifactId>pager-taglib</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- alibaba 数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- log -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
<build>
<finalName>ssm-web</finalName>
<plugins>
<!-- 编译插件,默认使用jdk1.8 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 使用utf-8编码处理资源文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
添加log4j配置文件 由于MyBatis依赖与log4j输出sql语句信息,所以需要配置log4j配置文件。
#设置输出级别和输出位置
log4j.rootLogger=debug,Console
#设置控制台相关的参数
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
#设置MyBatis的输出内容
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
配置web.xml
添加SpringMVC核心控制器DispatcheerServlet配置信息
<context:component-scan base-package=“com.znsd.ssm” />
<mvc:default-servlet-handler />
<mvc:annotation-driven />
配置文件上传
配置文件上传组件。
<context:property-placeholder location=“classpath:jdbc.properties” />
jdbc.properties文件
##jdbcu914Du7F6E##
jdbc.driver=com.mysql.jdbc.Driver
jdbc.dbname=test
jdbc.port=3306
jdbc.host=192.168.41.10
jdbc.user=znsd_test
jdbc.password=123456
jdbc.pool.minPoolSize=5
jdbc.pool.maxPoolSize=30
jdbc.pool.initialPoolSize=5
jdbc.pool.maxIdleTime=60
jdbc.pool.timeBetweenEvictionRunsMillis=60000
jdbc.pool.minEvictableIdleTimeMillis=300000
配置sqlSessionFactory
MyBatis中使用sqlSessionFactory来生产session对象,和Hibernate中相似,也可以交由spring来管理。
设置扫描的映射文件和dao包
classpath*:/mybatis-mapping/*.xml Mybatis映射文件 在SSM中,可以省略mybatis配置文件,也可以添加,只不过需要在sqlSessionFactory中配置configLocation属性即可。 <?xml version="1.0" encoding="UTF-8" ?> 配置事务 在Spring里面,可以通过AOP来管理事务,非常强大。<tx:advice id=“txAdvice” transaction-manager=“txManager”>
tx:attributes
<tx:method name=“find*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“search*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“query*” propagation=“REQUIRED” read-only=“true” />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="submit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
实现查询学生列表信息
使用SSM来查询学生列表信息
获取学生列表的json格式数据
image
添加实体类
学生实体类
public class Student implements Serializable {
private Integer id;
private String name;
private String password;
private String gender;
private String address;
private Clazz clazz;
// 省略getter和setter方法
}
映射文件
StudentDao接口
@Repository
public interface StudentDao {
public List selectList();
}
映射文件StudentMapper.xml
<resultMap id="BaseResultMap" type="com.znsd.ssm.bean.Student">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="gender" property="gender" jdbcType="VARCHAR" />
<result column="c_id" property="clazz.classId" jdbcType="INTEGER" />
</resultMap>
<select id="selectList" resultMap="BaseResultMap">
select * from t_student
</select>
Service层 StudentService代码
public interface StudentService {
public List selectList();
}
StudentService实现类
@Service
public class StudentServiceImpl implements StudentService {
@Resource
private StudentDao studentDao;
@Override
public List<Student> selectList() {
return this.studentDao.selectList();
}
}
Controller层
由控制器层将数据返回对应的视图,输出json格式的数据。
@Controller
@RequestMapping(“student”)
public class StudentController {
@Autowired
private StudentService service;
@RequestMapping("/list")
public String getStudentList(Map<String, Object> map) {
List<Student> studentList = service.selectList();
map.put("studentList", studentList);
return "studentList";
}
@ResponseBody
@RequestMapping("/json")
public List<Student> getlistJson() {
List<Student> list = service.selectList();
return list;
}
}
View视图层
显示学生列表
id | name | pass | sex |
${stu.id} | ${stu.name} | ${stu.password} | ${stu.gender} |
imageimage
总结
SSM整合
最后
以上就是文静电脑为你收集整理的Spring+Mybatis+SpringMVC框架集成的全部内容,希望文章能够帮你解决Spring+Mybatis+SpringMVC框架集成所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复