码迷,mamicode.com
首页 > 移动开发 > 详细

spring整合mybatis之mapper开发

时间:2018-03-02 20:43:02      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:frame   五步   actor   throw   drive   eth   cte   pass   编写   

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包。

技术分享图片

还有一个spring-test的测试包(spring-test\4.1.1.RELEASE)

 1.3   整合的步骤

第一步:创建一个java工程。

第二步:导入jar包。(上面提到的jar包)

第三步:mybatis的配置文件sqlmapConfig.xml

第四步:编写Spring的配置文件

1、数据库连接及连接池

2、事务管理(暂时可以不配置)

3、sqlsessionFactory对象,配置到spring容器中

4、mapeer代理对象或者是dao实现类配置到spring容器中。

第五步:编写dao或者mapper文件

第六步:测试。

 包结构:

技术分享图片

1.3.1 SqlMapConfig.xml

原先在sqlMapConfig.xml中的配置都交由spring管理(除了别名,spring不能处理)

<?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>
    <!-- 配置pojo别名 -->
    <typeAliases>
        <!-- 包扫描,默认别名是类名,不区分大小写 -->
        <package name="com.mybatis.po"/>
    </typeAliases>
</configuration>

1.3.2applicationContext.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: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>
    <!-- mapper配置 -->
    <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 加载mybatis的全局配置文件 -->
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
    </bean>
    
    <!-- 使用扫描包的形式来创建mapper代理对象 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.mybatis.mapper.UserMapper"></property>
    </bean>
</beans>

1.3.3  db.properties

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

1.4 UserMapper.java类

package com.mybatis.mapper;

import java.util.List;

import com.mybatis.po.User;

public interface UserMapper {
    //根据用户id查询用户信息
    public User findUserById(int id) throws Exception;
    //查询用户列表
    public List<User> findUserByUsername(String username) throws Exception;
    //添加用户信息
    public void insertUser(User user)throws Exception; 

}

1.5 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.mybatis.mapper.UserMapper">
<!-- 根据id获取用户信息 -->
    <select id="findUserById" parameterType="int" resultType="user">
        select * from user where id = #{id}
    </select>
<!-- 自定义条件查询用户列表 -->
    <select id="findUserByUsername" parameterType="java.lang.String" 
            resultType="user">
       select * from user where username like ‘%${value}%‘ 
    </select>
<!-- 添加用户 -->
    <insert id="insertUser" parameterType="user">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
        select LAST_INSERT_ID() 
    </selectKey>
      insert into user(username,birthday,sex,address) 
      values(#{username},#{birthday},#{sex},#{address})
    </insert>
</mapper>

1.6 扫描包形式配置mapper

<!-- 使用扫描包的形式来创建mapper代理对象 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.mybatis.mapper"></property>
    </bean>

每个mapper代理对象的id就是类名,首字母小写,多个包之间使用“,”间隔。

1.7   测试方法

package test;

import static org.junit.Assert.*;

import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.mybatis.mapper.UserMapper;
import com.mybatis.po.User;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext.xml")
public class UserMapperTest {
    
    @Autowired
    private UserMapper userMapper;

    @Test
    public void testFindUserById() throws Exception {
        User user = userMapper.findUserById(10);
        System.out.println(user);
    }

    @Test
    public void testFindUserByUsername() throws Exception {
        List<User> list = userMapper.findUserByUsername("%张%");
        for (User user : list) {
            System.out.println(user);
        }
    }

    @Test
    public void testInsertUser() throws Exception {
        User user = new User();
        user.setAddress("北京");
        user.setBirthday(new Date());
        user.setSex("2");
        user.setUsername("韩美美");
        userMapper.insertUser(user);
    }

}

 

spring整合mybatis之mapper开发

标签:frame   五步   actor   throw   drive   eth   cte   pass   编写   

原文地址:https://www.cnblogs.com/lxk233/p/8494772.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!