码迷,mamicode.com
首页 > 编程语言 > 详细

MyBatis - Spring框架整合

时间:2019-12-17 23:58:02      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:space   har   roo   util   target   别名   pre   size   instance   

目的:

使用 Spring 容器用单例模式管理 MyBatis 的 sqlSessionFactory ;
使用 Spring 管理连接池、数据源等;
将 Dao / Mapper 动态代理对象注入到 Spring 容器中,使用时直接获取;

整合的三种方式:链接??

https://www.jianshu.com/p/412051d41d73

实例

● 导入所需的Jar包

MyBatis+Spring整合包:mybatis-spring-1.3.2.jar
MyBatis核心包:mybatis-3.4.6.jar
Spring三方及依赖:下载
数据库连接驱动:mysql-connector-java-5.1.46-bin.jar、ojdbc7.jar
C3P0连接池:c3p0-0.9.5.2.jar、mchange-commons-java-0.2.11.jar

技术图片

创建Mybatissql主配置文件 - MapConfig.xml

<?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>

    <!-- 配置别名 -->
    <typeAliases>
        <package name="com.sikiedu.bean" />
    </typeAliases>

    <mappers>
        <package name="com.sikiedu.mapper" />
    </mappers>

</configuration>

● 创建Spring主配置文件applicationContext.xml

技术图片
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
 3     xmlns:util="http://www.springframework.org/schema/util"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 5     http://www.springframework.org/schema/beans/spring-beans.xsd 
 6     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
 7     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
 8     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
 9     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd ">
10 
11 
12     <!-- 读取properties文件 -->
13     <context:property-placeholder location="db.properties" />
14 
15     <!-- 配置C3P0数据源 -->
16     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
17         <property name="driverClass" value="${jdbc.driverClass}"></property>
18         <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
19         <property name="user" value="${jdbc.user}"></property>
20         <property name="password" value="${jdbc.password}"></property>
21     </bean>
22 
23     <!-- 配置MyBatis sqlSessionFactory -->
24     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
25         <!-- 引用数据源组件 -->
26         <property name="dataSource" ref="dataSource" />
27         <!-- 引用mybatis配置文件中的配置 -->
28         <property name="configLocation" value="classpath:sqlMapConfig.xml" />
29         <!-- 配置SQL映射文件信息 -->
30     </bean>
31 
32     <!-- UserDao -->
33     <bean id="userDaoImpl" class="com.sikiedu.dao.UserDaoImpl">
34         <!-- 将工厂注入到dao的父类 SQLSessionFactory -->
35         <property name="sqlSessionFactory" ref="sqlSessionFactory" />
36     </bean>
37 
38     <!-- Mapper动态代理开发 -->
39     <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
40         <!-- 注入 SQLSessionFactory -->
41         <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
42         <!-- 配置接口 -->
43         <property name="mapperInterface" value="com.sikiedu.mapper.UserMapper" />
44     </bean>
45 
46     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
47         <property name="basePackage" value="com.sikiedu.mapper" />
48     </bean>
49 
50 </beans>
applicationContext.xml

① 读取db.properties

技术图片
1 jdbc.driverClass=com.mysql.jdbc.Driver
2 jdbc.jdbcUrl=jdbc:mysql://localhost:3306/games_db?useUnicode=true&characterEncoding=UTF-8&useSSL=true
3 jdbc.user=root
4 jdbc.password=123456
db.properties
<context:property-placeholder location="db.properties" />

② 配置C3P0连接池

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driverClass}"></property>
    <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
    <property name="user" value="${jdbc.user}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>

③ 配置sqlSessionFactory

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 引用数据源组件 -->
    <property name="dataSource" ref="dataSource" />
    <!-- 引用mybatis配置文件中的配置 -->
    <property name="configLocation" value="classpath:sqlMapConfig.xml" />
    <!-- 配置SQL映射文件信息 -->
</bean>

④ 测试

技术图片
 1 package com.sikiedu.bean;
 2 
 3 import java.util.Date;
 4 import java.util.List;
 5 
 6 public class User {
 7     private Integer id;
 8 
 9     private String username;
10 
11     private String userpassword;
12 
13     private Long balance;
14 
15     private Date grgisterdate;
16 
17     public Integer getId() {
18         return id;
19     }
20 
21     public void setId(Integer id) {
22         this.id = id;
23     }
24 
25     public String getUsername() {
26         return username;
27     }
28 
29     public void setUsername(String username) {
30         this.username = username == null ? null : username.trim();
31     }
32 
33     public String getUserpassword() {
34         return userpassword;
35     }
36 
37     public void setUserpassword(String userpassword) {
38         this.userpassword = userpassword == null ? null : userpassword.trim();
39     }
40 
41     public Long getBalance() {
42         return balance;
43     }
44 
45     public void setBalance(Long balance) {
46         this.balance = balance;
47     }
48 
49     public Date getGrgisterdate() {
50         return grgisterdate;
51     }
52 
53     public void setGrgisterdate(Date grgisterdate) {
54         this.grgisterdate = grgisterdate;
55     }
56 
57     @Override
58     public String toString() {
59         return "User [id=" + id + ", username=" + username + ", userpassword=" + userpassword + ", balance=" + balance
60                 + ", grgisterdate=" + grgisterdate + "]";
61     }
62 
63 }
User.java
技术图片
1 package com.sikiedu.dao;
2 
3 import com.sikiedu.bean.User;
4 
5 public interface UserDao {
6 
7     public User selectByPrimaryKey(Integer id);
8 
9 }
UserDao.java
技术图片
 1 package com.sikiedu.dao;
 2 
 3 import org.apache.ibatis.session.SqlSession;
 4 import org.mybatis.spring.support.SqlSessionDaoSupport;
 5 
 6 import com.sikiedu.bean.User;
 7 
 8 public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
 9 
10     @Override
11     public User selectByPrimaryKey(Integer id) {
12         SqlSession session = getSqlSession();
13         return session.selectOne("com.sikiedu.mapper.UserMapper.selectByPrimaryKey", id);
14     }
15 
16 }
UserDaoImpl.java
<bean id="userDaoImpl" class="com.sikiedu.dao.UserDaoImpl">
    <!-- 将工厂注入到dao的父类 SQLSessionFactory -->
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// 传递ID  -  ac.getBean("userDaoImpl");
UserDaoImpl userDao = ac.getBean(UserDaoImpl.class);// 传递接口
User user = userDao.selectByPrimaryKey(1);
System.out.println(user);

Mapper动态代理开发

 - Mapper接口与Mapper.xml映射文件必须放在同一目录中,Mapper.xml的namespace必须为全路径包名。

技术图片

 - sqlMapConfig.xml配置 - 以包的形式扫描映射器的位置

<mappers>
    <package name="com.sikiedu.mapper" />
</mappers>

- spring主配置文件 - 将MapperFactoryBean交给Spring管理

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
    <!-- 注入 SQLSessionFactory -->
    <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    <!-- 配置接口 -->
    <property name="mapperInterface" value="com.sikiedu.mapper.UserMapper" />
</bean>

- 测试

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// 传递ID  -  ac.getBean("userMapper");
UserMapper userMapper = ac.getBean(UserMapper.class);// 传递接口
User user = userMapper.selectByPrimaryKey(2);
System.out.println(user);

Mapper动态扫描开发

- Mapper接口与Mapper.xml映射文件必须放在同一目录中,Mapper.xml的namespace必须为全路径包名。

技术图片

 - sqlMapConfig.xml配置 - 以包的形式扫描映射器的位置

<mappers>
    <package name="com.sikiedu.mapper" />
</mappers>

 - applicationContext.xml配置 - 使用MapperScannerConfigurer进行扫描

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.sikiedu.mapper" />
</bean>

 - 测试

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = ac.getBean(UserMapper.class);
User user = userMapper.selectByPrimaryKey(3);
System.out.println(user);

MyBatis - Spring框架整合

标签:space   har   roo   util   target   别名   pre   size   instance   

原文地址:https://www.cnblogs.com/Dm920/p/12053796.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!