标签:
mybatis与spring整合
方式1:通过原始dao方法(不使用代理),
方式2:通过代理方式
搭建运行环境
1)导入
mybatis的jar包--->源码中的核心包+依赖包
mybatis提供的与spring整合的包
spring的核心包+事务包+aop面向切面编程包+数据库jdbc包
数据库驱动+c3p0包
2)建立配置文件 包括spring的核心文件bean.xml 以及 mybatis的核心文件sqlMapConfig.xml
2.User
public class User { private int id; private String username; private Date birthday; private String sex;//省略getter setter
============================================普通Dao方式==========================================================================
3.配置文件User.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"> <!-- 这里的namespace命名空间,等于mapper接口的地址 --> <mapper namespace="test"> <select id="findUserById" parameterType="int" resultType="cn.itcast.domain.User"> select * from user where id=#{id} </select> </mapper>
package cn.itcast.dao; import java.util.List; import cn.itcast.domain.User; public interface UserDao { public User findUserById(int id); }5.UserDaoImp
实现SqlSessionDaoSupport接口就不用注入SqlSessionFactory了 因为这个接口中已经定义了 并且有setter方法
package cn.itcast.dao.impl; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.mybatis.spring.support.SqlSessionDaoSupport; import cn.itcast.dao.UserDao; import cn.itcast.domain.User; public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{ public User findUserById(int id) { SqlSession sqlSession = this.getSqlSession(); User user=sqlSession.selectOne("test.findUserById", id); return user; } }
============================================代理方式整合==========================================================================
下面是代理方式配置:
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"> <!-- 这里的namespace命名空间,等于mapper接口的地址 --> <mapper namespace="cn.itcast.mapper.UserMapper"> <select id="findUserById" parameterType="int" resultType="cn.itcast.domain.User"> select * from user where id=#{id} </select> </mapper>UserMapper接口
package cn.itcast.mapper; import cn.itcast.domain.User; public interface UserMapper { public User findUserById(int id); }
核心配置文件sqlMapperConfig.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> <mappers> <!-- 1.通过resource方法一次加载一个映射文件 --> <mapper resource="cn/itcast/mapper/User.xml" /> <mapper resource="cn/itcast/mapper/UserMapper.xml" /> </mappers> </configuration>
1)数据源的配置
2)sqlSessionFactory的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <!-- 1.配置数据源连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis01"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="user" value="root"></property> <property name="password" value="169500"></property> <property name="initialPoolSize" value="3"></property> <property name="maxPoolSize" value="6"></property> </bean> <!--2. 配置sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 加载sqlMapConfig.xml文件 --> <property name="configLocation" value="sqlMapConfig.xml"></property> <!-- 数据源 --> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="userDao" class="cn.itcast.dao.impl.UserDaoImpl"><!--普通Dao方式配置--> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> <!-- mapper代理方式配置 --> <!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> mapperInterface <property name="mapperInterface" value="cn.itcast.mapper.UserMapper"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> --> <!-- mapper代理配置包扫描的方式 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 指定扫描的包 --> <property name="basePackage" value="cn.itcast.mapper"></property> <!-- sqlSessionFactory的配置 --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>测试:
package cn.itcast.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.itcast.dao.UserDao; import cn.itcast.domain.User; import cn.itcast.mapper.UserMapper; public class Spring_Mybatis_Test { @Test public void test(){ ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml"); UserDao userDao=(UserDao) ac.getBean("userDao"); User user = userDao.findUserById(2); System.out.println(user.getAddress()); }@Test public void testmapper(){ ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml"); UserMapper userMapper= (UserMapper) ac.getBean("userMapper"); User user = userMapper.findUserById(2); System.out.println(user.getAddress()); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/u014010769/article/details/47304007