美文网首页
mybatis 10 与spring简单整合

mybatis 10 与spring简单整合

作者: 小小机器人 | 来源:发表于2016-10-18 19:26 被阅读26次

结构:

Paste_Image.png

jar包:

Paste_Image.png

配置文件:
myBatis-conf.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>
        <typeAlias type="com.xxjqr.entity.Dept" alias="Dept"/>
    </typeAliases>

    <mappers>
        <mapper resource="com/xxjqr/entity/DeptMapper.xml"/>
    </mappers>
</configuration>

applicationContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.xxjqr"></context:component-scan>

    <!-- 加载Proerties配置文件 -->
    <context:property-placeholder location="classpath:com/xxjqr/dao/db.properties"/>
    
    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driverClass}"></property>
        <property name="jdbcUrl" value="${jdbcUrl}"></property>
        <property name="user" value="${user}"></property>
        <property name="password" value="${password}"></property>
    </bean>
    
    <!-- 配置sqlSession工厂 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:myBatis-conf.xml"></property>
    </bean>
    
    <!-- 配置sqlSessionTemplate -->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
    </bean>

</beans>

DeptMapper.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.xxjqr.entity.DeptMapper"><!-- 当前包+文件名 -->
    <resultMap type="Dept" id="deptResultMap">
        <id column="dept_id" property="deptId"/>
        <result column="dept_name" property="deptName"/>
        <result column="dept_address" property="deptAddress"/>
    </resultMap>

    <insert id="insertDept" parameterType="Dept">
        insert into dept_t(dept_name,dept_address) values(#{deptName},#{deptAddress})
    </insert>
</mapper>

类:

package com.xxjqr.dao.impl;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;

import com.xxjqr.dao.DeptDao;
import com.xxjqr.entity.Dept;

import lombok.Data;

@Repository("deptDao")
@Data
public class DeptDaoImpl implements DeptDao {

    @Resource
    private SqlSessionTemplate sqlSessionTemplate;
    
    public int insertDept(Dept dept) {
        //命名空间+sqlId
        return sqlSessionTemplate.insert("com.xxjqr.entity.DeptMapper.insertDept",dept);
    }

}

package com.xxjqr.service.impl;


import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xxjqr.dao.DeptDao;
import com.xxjqr.entity.Dept;
import com.xxjqr.service.DeptService;

import lombok.Data;

@Service("deptService")
@Data
public class DeptServiceImpl implements DeptService {
    
    @Resource
    private DeptDao deptDao;
    
    public int insertDept(Dept dept) {
        deptDao.insertDept(dept);
        return 0;
    }

}

package com.xxjqr.Test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.xxjqr.entity.Dept;
import com.xxjqr.service.DeptService;

public class App {
    
    
    @Test
    public void testInsertDept(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml",App.class);
        DeptService deptService = (DeptService)ac.getBean("deptService");

        Dept dept = new Dept();
        dept.setDeptName("开发部");
        dept.setDeptAddress("贵州");
        System.out.println("受影响行数:"+deptService.insertDept(dept));
    }
}

优化

1. 优化别名
myBatis-conf.xml中修改

Paste_Image.png

2. 优化sql映射文件的加载
myBatis-conf.xml中

Paste_Image.png

applicationContext.xml中修改


Paste_Image.png

3. 优化dao实现

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    
    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.xxjqr"></context:component-scan>

    <!-- 加载Proerties配置文件 -->
    <context:property-placeholder location="classpath:db.properties"/>
    
    <!-- 配置数据源(使用MapperScannerConfigurer后就不能使用这种方式了???) -->
<!--    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> -->
<!--         <property name="driverClass" value="${driverClass}"></property> -->
<!--         <property name="jdbcUrl" value="${jdbcUrl}"></property> -->
<!--         <property name="user" value="${user}"></property> -->
<!--         <property name="password" value="${password}"></property> -->
<!--     </bean> -->

    <!-- 配置数据源,记得去掉myBatis-config.xml的数据源相关配置 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">            
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis01" />
        <property name="user" value="root" />
        <property name="password" value="221121" />     
    </bean>
    
    <!-- 配置sqlSession工厂 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:myBatis-conf.xml"></property>
        <!-- 配置扫描式加载SQL映射文件 -->
        <property name="mapperLocations" value="classpath:com/xxjqr/entity/*.xml"/>
    </bean>
    
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>



    <!-- 转换器 (可以省略dao的实现类) -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        <property name="basePackage" value="com.xxjqr.dao"/>
    </bean>
    <!-- 使用该配置可以生成 
    basepack包下的所有Mapper.xml中命名空间 对应 dao接口的代理对象;并注入到spring容器中
    sql的id就应该是dao接口中定义的方法名 -->
</beans>

<?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.xxjqr.entity.DeptMapper"> -->

<!-- mapper的namespace应该对应的是dao接口 -->
<mapper namespace="com.xxjqr.dao.DeptDao">
    <resultMap type="Dept" id="deptResultMap">
        <id column="dept_id" property="deptId"/>
        <result column="dept_name" property="deptName"/>
        <result column="dept_address" property="deptAddress"/>
    </resultMap>

    <!-- id对应的是dao接口定义的方法 -->
    <insert id="insertDept" parameterType="Dept">
        insert into dept_t(dept_name,dept_address) values(#{deptName},#{deptAddress})
    </insert>
</mapper>

使用该配置后,就无需Dao接口的实现了,因为框架会自动将dao接口的代理对象并注入到spring容器中;
DeptServiceImpl会通过@Autowired将代理对象注入到“dao接口”引用

相关文章

网友评论

      本文标题:mybatis 10 与spring简单整合

      本文链接:https://www.haomeiwen.com/subject/pkhtuttx.html