mybatis整合spring的完整过程
1.1 整合思路
1、SqlSessionFactory对象应该放到spring容器中作为单例存在。
2、传统dao的开发方式中,应该从spring容器中获得sqlsession对象。
3、Mapper代理形式中,应该从spring容器中直接获得mapper的代理对象。
4、数据库的连接以及数据库连接池事务管理都交给spring容器来完成。
1.2 整合需要的jar包
1、spring的jar包
2、Mybatis的jar包
3、Spring+mybatis的整合包。
4、Mysql的数据库驱动jar包。
5、数据库连接池的jar包。
1.3 整合的步骤
第一步:创建一个java工程。
第二步:导入jar包。(上面提到的jar包)
第三步:mybatis的配置文件sqlmapConfig.xml
第四步:编写Spring的配置文件
1、数据库连接及连接池
2、事务管理(暂时可以不配置)
3、sqlsessionFactory对象,配置到spring容器中
4、mapper代理对象或者是dao实现类配置到spring容器中。
第五步:编写dao或者mapper文件
第六步:测试。
1.3.1 SqlMapConfig.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.huida.po"/>
</typeAliases>
<!-- 在一个核心配置文件中只写一个mappers -->
<mappers>
<mapper resource="config/User.xml"/>
</mappers> </configuration>
1.3.2 db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
1.3.3 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 加载配置文件 -->
<context:property-placeholder location="classpath:config/db.properties" />
<!-- 数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean> <!-- 整合Sql会话工厂归spring管理 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定mybatis核心配置文件 -->
<property name="configLocation" value="classpath:config/SqlMapConfig.xml"></property>
<!-- 指定会话工厂使用的数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean> </beans>
1.4 Dao的开发
三种dao的实现方式:
1、传统dao的开发方式
2、使用mapper代理形式开发方式
3、使用扫描包配置mapper代理。
先创建User类存储数据库表user的所有字段:
package com.huida.po; import java.util.Date;
import java.util.List; public class User { private int id;
private String username;// 用户姓名
private String sex;// 性别
private Date birthday;// 生日
private String address;// 地址
public void setId(int id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public void setAddress(String address) {
this.address = address;
}
public int getId() {
return id;
}
public String getUsername() {
return username;
}
public String getSex() {
return sex;
}
public Date getBirthday() {
return birthday;
}
public String getAddress() {
return address;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + "]";
} }
1.4.1 传统dao的开发方式
接口+实现类来完成。需要dao实现类需要继承SqlsessionDaoSupport类。
1.4.1.1 Dao接口
package com.huida.dao;
import java.util.List;
import com.huida.po.User;
public interface UserDao {
public User findUserById(Integer id);
public List<User> findUserByUserName(String name);
}
1.4.1.2 Dao实现类
package com.huida.dao; import java.util.List; import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport; import com.huida.po.User; public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao { /*private SqlSessionFactory factory; public void setFactory(SqlSessionFactory factory) {
this.factory = factory;
}*/
@Override
public User findUserById(Integer id) {
//获取会话
//SqlSessionDaoSupport中已经注入了SqlSessionFactory,所以我们不需要在定义了
SqlSession session=getSqlSession();
User user=session.selectOne("test.findUserById",id);
//整合后会话归spring管理,所以不需要手动关闭
//session.close();
return user;
}
//模糊查询
@Override
public List<User> findUserByUserName(String name) {
SqlSession session=getSqlSession();
List<User> list=session.selectList("test.findUserByUsername", name);
return list;
} }
1.4.1.3 配置dao
将dao实现类配置到spring容器applicationContext.xml中。
<!--
配置原生Dao实现
注意:class必须指定Dao实现类的全路径名称
-->
<bean id="userDao" class="com.huida.dao.UserDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
1.4.1.4 测试方法
package comm.huida.test; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.huida.dao.UserDao;
import com.huida.po.User; public class UserDaoTest { private ApplicationContext applicationContext;
@Before
public void init(){
applicationContext=new ClassPathXmlApplicationContext("config/applicationContext.xml");
}
@Test
public void testFindUserById(){
UserDao userDao=(UserDao) applicationContext.getBean("userDao");
User user=userDao.findUserById(1);
System.out.println(user);
}
}
1.4.2 Mapper代理形式开发dao
1.4.2.1 开发mapper接口
package com.huida.mapper;
import com.huida.po.User;
public interface UserMapper {
public User findUserById(Integer id);
}
1.4.2.2 配置UserMapper
<?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接口代理实现编写规则:
1.映射文件中namespace要等于接口的全路径
2.通过sql语句实现数据库的操作
3.映射文件中sql语句id要等与于接口的方法名称
4.映射文件中传入参数类型要等于接口方法的传入参数类型
5.映射文件中返回结果集类型要等于接口方法的返回值类型
-->
<mapper namespace="com.huida.mapper.UserMapper"> <select id="findUserById" parameterType="java.lang.Integer" resultType="user">
<!-- select语句返回的是user对象,所以resultType中写User类的全路径 -->
select * from user where id=#{id}
</select> </mapper>
1.4.2.3 在applicationContext.xml中配置mapper代理
<!-- Mapper接口代理实现-->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
配置mapper接口的全路径名称
<property name="mapperInterface" value="com.huida.UserMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
1.4.2.4 测试方法
package comm.huida.test; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.huida.mapper.UserMapper;
import com.huida.po.User; public class UserMapperTest { private ApplicationContext applicationContext;
@Before
public void setUp(){
applicationContext=new ClassPathXmlApplicationContext("classpath:config/applicationContext.xml");
}
@Test
public void testFindUserById(){
UserMapper userMapper=(UserMapper) applicationContext.getBean("userMapper");
User user=userMapper.findUserById(1);
System.out.println(user);
}
}
1.4.3 扫描包形式配置mapper
在applicationContext.xml中将刚才的mapper配置更改为使用扫描包的方法:
<!-- 使用包扫描的方式批量引入Mapper
扫描后引用的时候可以使用类名,首字母小写. --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定要扫描的包的全路径名称,如果有多个包用英文状态下的逗号分隔 -->
<property name="basePackage" value="com.huida.mapper"></property>
</bean>
每个mapper代理对象的id就是类名(类名的首字母要小写)
mybatis整合spring的完整过程的更多相关文章
- mybatis整合Spring编码
mybatis整合Spring的核心代码 spring-dao.xml <?xml version="1.0" encoding="UTF-8"?> ...
- MyBatis整合Spring原理分析
目录 MyBatis整合Spring原理分析 MapperScan的秘密 简单总结 假如不结合Spring框架,我们使用MyBatis时的一个典型使用方式如下: public class UserDa ...
- mybatis整合spring 之 基于接口映射的多对一关系
转载自:http://my.oschina.net/huangcongmin12/blog/83731 mybatis整合spring 之 基于接口映射的多对一关系. 项目用到俩个表,即studen ...
- mybatis整合spring获取配置文件信息出错
描述:mybatis整合spring加载jdbc.properties文件,然后使用里面配置的值来 配置数据源,后来发现用户变成了admin- jdbc.properties的配置: 加载配置: 报错 ...
- JAVAEE——Mybatis第二天:输入和输出映射、动态sql、关联查询、Mybatis整合spring、Mybatis逆向工程
1. 学习计划 1.输入映射和输出映射 a) 输入参数映射 b) 返回值映射 2.动态sql a) If标签 b) Where标签 c) Sql片段 d) Foreach标签 3.关联查询 a) 一对 ...
- Mybatis整合Spring -- typeAliasesPackage
Mybatis整合Spring 根据官方的说法,在ibatis3,也就是Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对Mybatis3的支持. 因此由M ...
- 160330、Mybatis整合Spring
转自csdn文章 http://haohaoxuexi.iteye.com/blog/1843309 Mybatis整合Spring 根据官方的说法,在ibatis3,也就是Mybatis3问世之前, ...
- (转)MyBatis框架的学习(六)——MyBatis整合Spring
http://blog.csdn.net/yerenyuan_pku/article/details/71904315 本文将手把手教你如何使用MyBatis整合Spring,这儿,我本人使用的MyB ...
- 不需要怎么修改配置的Mybatis整合Spring要点
首先对于Mybatis的主配置文件,只需要修改一处地方,将事务交给Spring管理,其它地方可以原封不动. <?xml version="1.0" encoding=&quo ...
随机推荐
- HDU 3667
http://acm.hdu.edu.cn/showproblem.php?pid=3667 最小费用最大流 本题流量和费用不是线性关系,fee=a*flow*flow,所以常规套模板spfa无法得到 ...
- js:关闭当前页面
var userAgent = navigator.userAgent; if (userAgent.indexOf("Firefox") != -1 || userAgent.i ...
- 【转载】python安装numpy和pandas
转载:原文地址 http://www.cnblogs.com/lxmhhy/p/6029465.html 最近要对一系列数据做同比比较,需要用到numpy和pandas来计算,不过使用python安装 ...
- web程序2
.
- C#:进程、线程、应用程序域(AppDomain)与上下文分析
进程 进程是操作系统用于隔离众多正在运行的应用程序的机制.在.Net之前,每一个应用程序被加载到单独的进程中,并为该进程指定私有的虚拟内存.进程不能直接访问物理内存,操作系统通过其它的处理把这 ...
- Python——内置函数(待完善)
内置函数(68个),分为六大类 思维导图: 1. 迭代器/生成器相关(3个) (1)range for i in range(10): #0-9 print(i) for i in range(1,1 ...
- 智能家居入门DIY——【六、使用OneNet后台处理数据】
OneNet使用起来要比lewei50复杂一些,它没有前台需要自己开发.命令下发也和之前介绍的lewei50有一些区别,这里着重介绍一下使用MQTT协议来进行通讯. 一.准备 1.Esp8266开发板 ...
- (转)为C# Windows服务添加安装程序
本文转载自:http://kamiff.iteye.com/blog/507129 最近一直在搞Windows服务,也有了不少经验,感觉权限方面确定比一般程序要受限很多,但方便性也很多.像后台运行不阻 ...
- 面试总结之Linux/Shell
Linux Linux cshrc文件作用 Linux如何起进程/查看进程/杀进程 Linux 文件755 代表什么权限 Linux辅助线程 Linux进程间通信方法 pipeline,msgq... ...
- Golang后台开发初体验
转自:http://blog.csdn.net/cszhouwei/article/details/37740277 补充反馈 slice 既然聊到slice,就不得不提它的近亲array,这里不太想 ...