具体的感兴趣可以参考:MyBatis

此时此刻,没用的话不再多说了,直接开始代码工程吧!

整体的代码实现:

具体使用到的我们在进行细说

基本上理解一边就能会使用整合

 准备工作:

 db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3307/shopping
jdbc.username=root
jdbc.password=12345

 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
applivationContext.xml
mvc,context,aop,tx,beans...命名约束
     <!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties" />

使用dbcp数据库连接池

<!-- 数据源,使用dbcp -->
<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>
配置SqlSessionFactory

不需要在类中单独进行配置

<!-- sqlSessinFactory -->
<!-- org.mybatis.spring.SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载mybatis的配置文件 -->
<property name="configLocation" value="mybatis/SqlMapConfig.xml" />
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>

 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.MrChengs.po"/>
</typeAliases> <!-- 加载 映射文件 -->
<mappers>
<mapper resource="sqlmap/User.xml"/>
</mappers>
</configuration>

其他的设置项在使用的时候在进行添加,,,,,,

 一.dao开发

 Userser.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="test">
<select id="findUserByID" parameterType="int" resultType="com.MrChengs.po.User">
select * from user where id=#{id}
</select>
</mapper>

UserDao.java

public interface UserDao {
//根据id查询用户信息
public User findUserById(int id) throws Exception;
}

 UserDaoImp.java

让实现接口类的类继承SqlSessionDaoSupport
通过spring进行注入,声明配置方式,配置dao的bean
 
此时不需要手动配置SqlSessionFactory
直接可以得到其对象
 
此时不需要手动关闭,在spring容器池里会自动关闭
public class UserDaoImp extends SqlSessionDaoSupport implements UserDao {
@Override
public User findUserById(int id) throws Exception {
SqlSession sqlSession = this.getSqlSession();
User user =sqlSession.selectOne("test.findUserByID", id);
return user;
}
}
applivationContext.xml
注入sqlSessionFactory
     <bean id="userDao" class="com.MrChengs.UserDao.UserDaoImp">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

测试:

public class Test {
private ApplicationContext applicationContext;
public ApplicationContext getApplication(){
applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
return applicationContext;
}
//Dao开发
@org.junit.Test
public void testDao() throws Exception{
UserDao userdao = (UserDao) getApplication().getBean("userDao");
User user = userdao.findUserById(1);
System.out.println(user);
}
}
DEBUG [main] - ==>  Preparing: select * from user where id=?
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <== Total: 1
DEBUG [main] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3bf7ca37]
DEBUG [main] - Returning JDBC Connection to DataSource
User [id=1, username=王五, birthday=null, sex=2, address=null]

二.Mapper代理开发

UserMapper.java

//相当于dao接口
public interface UserMapper {
//根据id查询用户信息
public User findUserById(int id) throws Exception;
}

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.MrChengs.mapper.UserMapper">
<select id="findUserById" parameterType="int" resultType="com.MrChengs.po.User">
SELECT * FROM USER WHERE id=#{value}
</select>
</mapper>

注意上述的两个文件在一个路径下。

SqlMapConfig.xml

     <!-- 加载 映射文件 -->
<mappers>
<mapper resource="sqlmap/User.xml"/>
<package name="com.MrChengs.mapper"/>
</mappers>

通过MapperFactoryBean创建代理对象

<!-- mapper配置
MapperFactoryBean:根据mapper接口生成代理对象
-->
<!-- mapperInterface指定mapper接口 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.MrChengs.mapper.UserMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

测试:

public class TestMapper {
private ApplicationContext applicationContext;
public ApplicationContext getApplication(){
applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
return applicationContext;
}
//Mapper开发
@org.junit.Test
public void testmapper() throws Exception{
UserMapper user = (UserMapper) getApplication().getBean("userMapper");
User u = user.findUserById(1);
System.out.println(u);
}
}
DEBUG [main] - ==>  Preparing: SELECT * FROM USER WHERE id=?
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <== Total: 1
DEBUG [main] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7c729a55]
DEBUG [main] - Returning JDBC Connection to DataSource
User [id=1, username=王五, birthday=null, sex=2, address=null]

如果有很多个类都需要对每个mapper进行配置,此时需要大量的工程???

     <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.MrChengs.mapper.UserMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
。。。。。

通过MapperScannerConfigurer进行mapper扫描(建议使用)

applicationContext.xml

说明:

 
mapper批量扫描,从mapper包中扫描出mapper接口,自动创建代理对象并且在spring容器中注册
遵循规范:将mapper.java和mapper.xml映射文件名称保持一致,且在一个目录 中
自动扫描出来的mapper的bean的id为mapper类名(首字母小写
 
name="basePackage"
指定扫描的包名
如果扫描多个包,每个包中间使用半角逗号分隔
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.MrChengs.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean>

其余的测试不变......

MyBatis(9)整合spring的更多相关文章

  1. MyBatis之整合Spring

    MyBatis之整合Spring 整合思路: 1.SqlSessionFactory对象应该放到spring容器中作为单例存在 2.传统dao的开发方式中,应该从spring容器中获得sqlSessi ...

  2. 【Mybatis】MyBatis之整合Spring(八)

    创建环境 系统:macOS Java:1.8 软件:eclipse,maven,mysql 创建步骤 本例:创建一个Maven项目(SpringMVC+Spring+Mybatis),页面上展示员工列 ...

  3. Mybatis整合Spring

    根据官方的说法,在ibatis3,也就是Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对Mybatis3的支持.因此由Mybatis社区自己开发了一个My ...

  4. 框架整合——Spring与MyBatis框架整合

    Spring整合MyBatis 1. 整合 Spring [整合目标:在spring的配置文件中配置SqlSessionFactory以及让mybatis用上spring的声明式事务] 1). 加入 ...

  5. Mybatis整合Spring -- typeAliasesPackage

    Mybatis整合Spring 根据官方的说法,在ibatis3,也就是Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对Mybatis3的支持. 因此由M ...

  6. 160330、Mybatis整合Spring

    转自csdn文章 http://haohaoxuexi.iteye.com/blog/1843309 Mybatis整合Spring 根据官方的说法,在ibatis3,也就是Mybatis3问世之前, ...

  7. MyBatis 学习-与 Spring 集成篇

    根据官方的说法,在 ibatis3,也就是 Mybatis3 问世之前,Spring3 的开发工作就已经完成了,所以 Spring3 中还是没有对 Mybatis3 的支持.因此由 Mybatis 社 ...

  8. SSM框架-----------SpringMVC+Spring+Mybatis框架整合详细教程

    1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One  ...

  9. mybatis入门_一对多,多对多映射以及整合spring框架

    一.一对多映射. 1.1 一对多映射之根据多的一方关联查询一的一方 示例:查询出具体的订单信息,同时也查询出来订单的用户信息. 引入的订单表如下所示: 框选出来的为具体的外键. 订单的Pojo类如下所 ...

  10. 1.springMVC+spring+Mybatis的整合思路

    SSM整合的过程:就是把一些东西交给spring管理,也就是添加配置文件的一个过程.那么有哪些东西我们要交给spring管理呢?大概有以下几个: 1.数据源(可配置数据库连接池) 2.SqlSessi ...

随机推荐

  1. Effective C++ .08 别让异常逃离析构函数

    异常不怎么用,C++能自己控制析构过程,也就有这个要求了.容器不能完全析构其中的元素真是太危险了

  2. Redis(MySQL和redis怎么分工合作的?)

    新手: redis也是服务器,主要部署在服务器上缓解服务器的压力,对于访问量交大的数据可以先缓存到redis,用户访问时直接访问redis,不用再直接访问数据库,缓解数据库的压力.mysql主要存储数 ...

  3. 11.1NOIP模拟赛解题报告

    心路历程 预计得分:\(100 + 100 + 50\) 实际得分:\(100 + 100 + 50\) 感觉老师找的题有点水呀. 上来看T1,woc?裸的等比数列求和?然而我不会公式呀..感觉要凉 ...

  4. cf1043D. Mysterious Crime(枚举)

    题意 题目链接 给出\(m\)个长度为\(n\)的排列,问有多少连续公共子串 \(m \leqslant 10, n \leqslant 10^5\) Sol 非常naive的一道题然而交了3遍才过( ...

  5. Ant design 项目打包后报错:"Menu(or Flex) is not defined"

    我的项目使用了ant-design 和 ant-design-mobile,在测试环境上没问题,但是打包发布之后控制台报错 Menu is not defined Flex is not define ...

  6. Bootstrap + AngularJS+ Ashx + SQL Server/MySQL

    去年年底12月,为适应移动端浏览需求,花了1个月时间学习Bootstrap,并将公司ASP网站重构成ASP.NET. 当时采取的网站架构: Bootstrap + jQuery + Ashx + SQ ...

  7. eclipse安装checkStyle

    今天用eclipse mars 安装checkstyle 代码测试工具,安装完后重启竟然没有,最后发现原来是 自己安装的步骤错了,记录下. 1. 我的版本是:Version: Mars.2 Relea ...

  8. Oracle基础之Merge into

    Merge into语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句. 通过MERGE语句,根据一张表或多表联合查询的连接条件对另外一张表进行查询,连接条件匹配上的进行UPDA ...

  9. MySQL案例之Timestamp和Datetime

    mysql数据库常用的时间类型有timestamp和datetime,两者主要区别是占用存储空间长度不一致.可存储的时间也有限制,但针对不同版本下,timestamp字段类型的设置需要慎重,因为不注意 ...

  10. 阿里云OSS 上传文件SDK

    Aliyun OSS SDK for C# 上传文件 另外:查找的其他实现C#上传文件功能例子: 1.WPF用流的方式上传/显示/下载图片文件(保存在数据库) (文末有案例下载链接) 2.WPF中利用 ...