两大热门框架 Spring 与 Mybatis 如何整合呢?

102 阅读2分钟

整合的方式

  • 新建 maven 项目

  • 引入依赖包

  • 配置资源文件

案例实操

新建 maven 项目

新建 maven 项目 spring_mybatis

目录结构如下:

主目录包:

com.xxx.dao、

com.xxx.mapper、

com.xxx.service、

com.xxx.service.impl

测试包:spring_mybatis

引入依赖包

打开 pom.xml 开始添加依赖包


4.0.0

com.xxx
test-xxxms
1.0-SNAPSHOT

test-xxxms

www.example.com

UTF-8 1.7 1.7 junit junit 4.11 test org.springframework spring-context 4.3.2.RELEASE org.springframework spring-test 4.3.2.RELEASE org.springframework spring-jdbc 4.3.2.RELEASE org.springframework spring-tx 4.3.2.RELEASE c3p0 c3p0 0.9.1.2 org.mybatis mybatis 3.4.1 org.mybatis mybatis-spring 1.3.0 mysql mysql-connector-java 5.1.39 org.slf4j slf4j-log4j12 1.7.2 org.slf4j slf4j-api 1.7.2 tpl-web src/main/java **/*.xml src/main/resources **/*.xml **/*.properties

配置资源文件

a) Spring 文件 spring.xml

b) Mybatis 文件 mybatis.xml

c) 数据库连接 properties 文件 db.properties

d) 日志输出文件 log4j.properties

spring.xml 文件配置

<context:component-scan base-package="com.xxx"/>

<context:property-placeholder location="db.properties"/>

<tx:annotation-driven transaction-manager="txManager"/>



mybatis.xml 文件配置

db.properties 文件配置(对于其它数据源属性配置,见 c3p0 配置讲解,这里采用默认属性配置)

建立数据库 mybatis(注意数据库,用户名,密码以自己本地数据库为准

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=

utf8

jdbc.username=root

jdbc.password=

log4j.properties

便于控制台日志输出

Global logging configuration

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

扩展

开始编写 helloworld

User 实体类定义

public class User {

private int id;

private String userName;

private String userPwd;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getUserPwd() {

return userPwd;

}

public void setUserPwd(String userPwd) {

this.userPwd = userPwd;

}

@Override

public String toString() {

return "User [id=" + id + ", userName=" + userName + ", userPwd="

+ userPwd + "]";

}

}

UseDao 接口与映射文件定义

UserDao 接口

public interface UserDao {

public User queryUserById(int id);

}

UserMapper.xml(注意:此时映射文件命名空间定义要符合规则:接口包名.接口类

,否则不按规则出牌,测试会报错,然后你就蒙圈了!!!)

select id,userName,userPwd from user where id=#{id}

UserService 接口类与实现类定义

public interface UserService {

public User queryUserById();

}

UserServiceImpl 实现类(此时直接注入我们的 UserDao 接口即可,然后直接调用

其方法,事已至此,离成功仅差一步!)

@Service

public class UserServiceImpl implements UserService{

@Resource

private UserDao userDao;

public User queryUserById(){

return userDao.queryUserById(7);

}

}

junit 测试

因为与 spring 框架集成,我们采用 spring 框架测试 spring Test

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = {"classpath:spring.xml"} )

public class TestSpringMybatis {

@Autowired

private UserService userService;

@Test

public void testQueryUserById() {

System.out.println(userService.queryUserById(1));

}

}

结果输出

![](https://i0.hdslb.com/bfs/article/df23f8964ff5760fb80fec0ac2abe47e69aee7ce.png)