MyBatis和Spring进行整合

来源:互联网 发布:linux怎么建站 编辑:程序博客网 时间:2024/05/22 08:15

编写UserMapper.java:

package cn.cstor.mapper;import cn.cstor.pojo.User;public interface UserMapper {    public User selectUserById(Integer id);}

编写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="cn.cstor.mapper.UserMapper">    <select id="selectUserById" parameterType="Integer" resultType="User">        select * from user where id = #{id}    </select></mapper>

编写db.properties:

jdbc.driver:com.mysql.jdbc.Driverjdbc.url:jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8jdbc.username:rootjdbc.password:root

编写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="cn.cstor.pojo"/>    </typeAliases>    <!-- 载入mapper文件 -->    <mappers>        <package name="cn.cstor.mapper"></package>    </mappers></configuration>

编写application.xml:

<?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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.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}"/>    </bean>    <!-- MyBatis工厂 -->    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource"/>        <!-- 核心配置文件 -->        <property name="configLocation" value="classpath:sqlMapConfig.xml"/>    </bean>    <!-- Mapper动态代理开发 -->    <!--         <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">            <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>            <property name="mapperInterface" value="cn.cstor.mapper.UserMapper"/>        </bean>     -->    <!-- Mapper动态代理开发:扫描 -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <!-- 基本包 会自动扫描该包及其子包 -->        <property name="basePackage" value="cn.cstor.mapper"/>    </bean></beans>

编写测试类:

package cn.cstor.junit;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import cn.cstor.mapper.UserMapper;import cn.cstor.pojo.User;public class UserTest {//  配合Mapper动态代理//  @Test//  public void test(){//      ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");//      UserMapper mapper = (UserMapper) ac.getBean("userMapper");//      User user = mapper.selectUserById(110);//      System.out.println(user);//  }//  配合Mapper动态代理之扫描    @Test    public void test1() {        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");        UserMapper mapper = ac.getBean(UserMapper.class);        User user = mapper.selectUserById(110);        System.out.println(user);    }}

:绝大多数情况下使用Mapper动态代理之扫描 ,因为其直接扫描整个包下所有的Mapper,省去了多次定义的麻烦。

附录:

目录结构:

这里写图片描述

相关JAR包:

这里写图片描述

原创粉丝点击