Spring 整合 MyBatis

来源:互联网 发布:阿里云 混合云 编辑:程序博客网 时间:2024/06/08 09:43
本文主要介绍了如何将mybatis和spring整合在一起使用,本人使用的是mybatis-3.2.7 + spring4.1.3,使用c3p0作为数据库连接池。
1.编写数据访问接口(UserMapper.java)
public interface UserMapper {    public User findUserById(Integer id);}
2.编写数据访问接口映射文件(UserMapper.xml)
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd";><mapper namespace="com.itheima.mybatis.mapper.UserMapper">    <!-- 通过ID查询一个用户 -->    <select id="findUserById" parameterType="Integer" resultType="User">         select * from user where id = #{v}    </select>    </mapper>
3.编写mybatis配置文件(sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd";><configuration>    <!-- 设置别名 -->    <typeAliases>         <!-- 2. 指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 -->         <package name="com.itheima.mybatis.pojo" />    </typeAliases>        <mappers>         <package name="com.itheima.mybatis.mapper"/>    </mappers></configuration>
4.编写spring配置文件(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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.0.xsd";>    <context:property-placeholder location="classpath: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>        <!-- Mybatis的工厂 -->    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">         <property name="dataSource" ref="dataSource"/>         <!-- 核心配置文件的位置 -->         <property name="configLocation" value="classpath:sqlMapConfig.xml"/>    </bean>        <!-- Dao原始Dao -->    <bean id="userDao" class="com.itheima.mybatis.dao.UserDaoImpl">         <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>    </bean>    <!-- Mapper动态代理开发 -->    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">         <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>         <property name="mapperInterface" value="com.itheima.mybatis.mapper.UserMapper"/>    </bean>        <!-- Mapper动态代理开发   扫描 -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">         <!-- 基本包 -->         <property name="basePackage" value="com.itheima.mybatis.mapper"/>    </bean></beans>
5.数据库连接(db.properties)
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8jdbc.username=rootjdbc.password=root
6.测试
public class JunitTest {    @Test    public void testMapper() throws Exception {         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");         UserMapper mapper = ac.getBean(UserMapper.class);         User user = mapper.findUserById(10);         System.out.println(user);    }}

原创粉丝点击