Spring4+Mybatis3+SpringMVC(基于注解)整合步聚:
一)Mybatis3测试;
二)Mybatis3+Spring4整合;
三)Mybatis3+Spring4+SpringMVC整合;
一、新建maven web项目
参考maven项目文章--Maven新建Web项目
二、添加pom.xml依赖包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ssm.demo1</groupId>
<artifactId>ssmdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ssmdemo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--
spring版本号 -->
<spring.version>4.0.2.RELEASE</spring.version>
<!--
mybatis版本号 -->
<mybatis.version>3.2.6</mybatis.version>
<!--
log4j日志文件管理包版本 -->
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.12</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<!-- 表示开发的时候引入,发布的时候不会加载此包 -->
<scope>test</scope>
</dependency>
<!-- spring核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</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>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</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-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mybatis/spring包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- 导入java ee jar 包 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<!-- 导入Mysql数据库链接jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!-- JSTL标签类 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 日志文件管理包 -->
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- 格式化对象,方便输出日志 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->
<!-- 映入JSON -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- 上传组件包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
再clear下项目,就会自动从网上下载依赖包,本地maven仓库没有滴
三、在src/main/java/目录下新建如下包名
com.ssm.demo2 //
com.ssm.demo2.controller //存放@controller 注解的类
com.ssm.demo2.mapper //mybatis接口类与mapper(*.xml)
com.ssm.demo2.po //实体类
com.ssm.demo2.service //业务类
四、在src/main/resources目录新建如下包名
mybatis //存放mybatis的配置文件,在整合后,此文件下将无效
spring //存放spring的配置文件
五、在src/main/resources目录新建
db.properties //数据库连接信息配置
log4j.properties //日志打印配置
六、db.properties添加内容如下:
jdbc.user=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///mybatis?useUnicode=true&characterEncoding=utf8
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
注明:”
?useUnicode=true&characterEncoding=utf8”设置编码为utf-8
如不配置,有时数据库编码没配好,就可能出现查询中文,不出结果的情况, 以防万一配上!
七、Log4j.properties添加内容如下:
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
注明:在正式发布时,把debug改成info,该文件放在classpath目录下即可,框架会自动加载,
无须手工来加载!
项目结构如下:
八、新建数据库 用Navicat for
mysql 工具
8.1)、新建user数据表,结构如下
自行输入几条测试数据!
这样数据库就建好了!
九、在scr/main/resources中的mybatis目录下
新建mybatis_cofig.xml文件并添加如下
内容:
注:在与spring整合后,该文件下的配置将交由spring管理!
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 加载数据源配置 -->
<properties resource="db.properties" />
<!-- 别名 -->
<typeAliases>
<typeAlias alias="User" type="com.ssm.demo2.po.User"/>
</typeAliases>
<!--配置jdbc -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClass}"/>
<property name="url" value="${jdbc.jdbcUrl}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
</configuration>
十、接下来写po实体类与mapper文件
在po包中新建User.java类:
public class User {
private int id;
private String
username;
private Date password;
private char sex;
private String
address;
…setter/getter方法省略
}
十一、新建mapper接口类及映射文件(采用注解和代理接口方式,原始接口的可查看其他文章)
新建映射文件
在src/main/resources/mybatis包下新建 UserMapper.xml
内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ssm.demo2.mapper.UserMapper">
<!--查找记录 -->
<select id="findUserList" parameterType="java.lang.String" resultType="User">
select * from user where username
like "%"#{value}"%"
</select>
</mapper>
在mapper包下新建UserMapper.java接口文件
内容如下:
public interface UserMapper {
public
List<User>findUserList(String name) throws
Exception;
}
十二、配置映射文件(注册mapper)
在mybatis_config.xml中添加如下内容:
<mappers>
<!-- 加载mapper -->
<mapper resource="mybatis/UserMapper.xml"
/>
</mappers>
十三、添加测试方法
在src/test/main下新建com.ssm.demo2包并新键 MybatisTest.java文件
内容:
public class MybatisTest
{
private
SqlSessionFactory factory;
{
String config ="mybatis/mybatis_config.xml";
InputStream in;
try {
in =
Resources.getResourceAsStream(config);
factory=new
SqlSessionFactoryBuilder().build(in);
} catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void test()
throws Exception {
System.out.println(factory);
SqlSession session=factory.openSession();
UserMapper usero=session.getMapper(UserMapper.class);
List<User>users=usero.findUserList("www");
System.out.println(users.size());
}
}
运行并得到打印结果
上面有显示,查询的sql语句!如在数据中有结果,查询无数据,可运行上面的sql来排错!
十四、把在mybatis_config.xml中手工注册的mapper映射,改成,自动扫描式的
在mybatis_config.xml中的<mappers>标签下添加如下内容
<!--
注解的方式
自动扫描mapper包下的所有文件, 加载mapper
但是mapper中.xml必须与接口名字一样
-->
<package name="com.ssm.demo2.mapper" />
注:这样配置必须满足二个条件就是mapper接口类与mapper映射文件(*.xml)要同名,并在同一个包下!
把resources下mybatis目录下的UserMapper.xml复制至com.ssm.demo2.mapper包下
并在mybatis_config.xml中注释掉
<!-- 加载mapper
-->
<!--
<mapper resource="mybatis/UserMapper.xml" /> -->
结构如下:
其他的文件无须改动,再运行,也ok!
现在SSM整合的第一个框架已搭建好!
有关详细的mybatis请自行查阅相关文章!此文章只是针对整合!
下面在引基础上,整合spring框架,最后再整合springmvc!!