springMVC整合Mybatis

不前序了,直接上干货

1、先看一下applicationContext.xml文件配置(src目录下)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:tx="http://www.springframework.org/schema/tx"
 6     xmlns:context="http://www.springframework.org/schema/context"
 7     xsi:schemaLocation="
 8         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 9         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
10         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
11         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
12     <!-- 配置数据源 -->
13     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
14         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
15         <property name="url" value="jdbc:mysql://localhost:3306/863pro"/>
16         <property name="username" value="root"/>
17         <property name="password" value="123456"/>
18     </bean>
19     
20     <!-- 声明式事务配置 开始 -->
21     <!-- 配置事务管理器 -->
22     <!-- <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
23         <property name="dataSource" ref="dataSource"/>
24     </bean> -->
25     <!-- 配置事务通知 -->
26     <!-- <tx:advice id="txAdvice" transaction-manager="txManager">
27         <tx:attributes>
28             配置哪些方法使用什么样的事务,配置事务的传播特性
29             <tx:method name="add" propagation="REQUIRED"/>
30             <tx:method name="insert" propagation="REQUIRED"/>
31             <tx:method name="update" propagation="REQUIRED"/>
32             <tx:method name="delete" propagation="REQUIRED"/>
33             <tx:method name="remove*" propagation="REQUIRED"/>
34             <tx:method name="get" read-only="true"/>
35             <tx:method name="*" propagation="REQUIRED"/>
36         </tx:attributes>
37     </tx:advice>
38     <aop:config> 
39         <aop:pointcut expression="execution(* com.test.dao.impl.*.*(..))" id="pointcut"/>
40         <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
41     </aop:config> -->
42     <!-- 声明式事务配置 结束 -->
43     
44     <!-- 配置sqlSessionFactory -->
45     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
46         <property name="dataSource" ref="dataSource"/>
47         <property name="configLocation" value="classpath:mybatis.cfg.xml"></property>
48     </bean>
49     
50     <context:component-scan base-package="com.test" />
51 
52 </beans>

  中间注释掉的一大部分是事务管理的配置,实际的项目中是必不可少的,我没有用到,就注释掉了,感兴趣可以照着这个写,是正确的。要使用事务管理,需要在开头引入4、5、9、10这四行就OK了。

  最下边一行<context:component-scan base-package="com.test" />是spring提供的自动装配配置,如在本例中当做装配时,就会到com/test下搜索所有子目录,并找出符合条件的dao文件。

  配置数据源是链接数据库的配置,直接复制就ok,最下边的sqlSessionFactory配置是配置数据源,其中

 <property name="configLocation" value="classpath:mybatis.cfg.xml"></property>

是配置mybatis的重要配置,这个applicationContext.xml放在src目录下就ok。
2、mybatis.cfg.xml配置(src目录下,也就是和applicationContext.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="com.test.vo"/>
    </typeAliases>
    
    <mappers>
        <mapper resource="com/test/vo/user.mapper.xml"/>
    </mappers> 
</configuration>

  其中<package name="com.test.vo"/>是model所在的文件位置(比如你定义了User),<mapper resource="com/test/vo/user.mapper.xml"/>是user.mapper.xml文件位置,这个xml文件是直接对数据库文件进行配置的,下面会说到。整合mybatis必须要配置这个mybatis.cfg.xml文件,并导入相关jar包(后面会有)。

3、user.mapper.xml文件(com.test.vo,和model同级) 

<?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.test.vo.user.mapper">
    <select id="select" resultType="RelatedId">
        select boot_id,action_id,action_type,allowed,sender_id,sender_type,sender_attr,receiver_id,receiver_type,receiver_attr from program_log where id=#{id}
    </select>
    
    <insert id="add" parameterType="RelatedId" useGeneratedKeys="true">
        insert into test_program_log(time,machine_id,boot_id,action_id,action_type,allowed,sender_id,sender_type,sender_attr,receiver_id,receiver_type,receiver_attr) values(#{beginTime},#{machine_id},#{boot_id},#{action_id},#{action_type},#{allowed},#{sender_id},#{sender_type},#{sender_attr},#{receiver_id},#{receiver_type},#{receiver_attr})
    </insert>
    
    <resultMap type="LabelDB" id="getLabelDB">  
        <!-- 主键必须用id标签映射,其他的用非集合用result映射 集合类用collection映射 -->  
        <!-- column为数据库字段名,property为实体类属性名 -->   

           <result column="id" property="sid" />
        <result column="time" property="updateTime" /> 
        <result column="machine_id" property="machine_id" />      
    </resultMap>
    
    <select id="label_select" resultMap="getLabelDB">
        select id,time,machine_id from test_program_log where id=#{id} 
    </select>
    
    <insert id="label_add" parameterType="LabelDB" useGeneratedKeys="true">
        insert into label_db(time,sid,machine_id,sys_label,net_label) values(#{updateTime},#{sid},#{machine_id},#{sys_label},#{net_label})
    </insert>
</mapper>

  这个配置很清楚,<select>下就是做查询,<insert>下就是做插入等等,其中的id很重要,调用某一条数据库语句都是通过id,而在select中的resultType属性是返回类型,会自动同名赋值,如果model变量名与数据库字段不同,就用<resultMap>配置。

4、在com.test.dao.impl中的实现

package com.test.dao.impl;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.test.dao.RelatedIdDao;
import com.test.vo.LabelDB;
import com.test.vo.RelatedId;

@Repository("relatedIdDao")
public class RelatedIdDaoImpl extends SqlSessionDaoSupport implements RelatedIdDao {
    
    @Autowired
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        super.setSqlSessionFactory(sqlSessionFactory);
    }
    @Override
    public RelatedId selectRelatedId(int id) {
        RelatedId relatedId = new RelatedId();        
        relatedId = (RelatedId)this.getSqlSession().selectOne("com.test.vo.user.mapper.select", id);
        return relatedId;
    }
    
    @Override
    public LabelDB selectLabelDB(int id) {
        LabelDB labelDB = new LabelDB();
        labelDB = (LabelDB)this.getSqlSession().selectOne("com.test.vo.user.mapper.label_select", id);
        return labelDB;
    }

    @Override
    public void insertLog(RelatedId relatedId) {
        this.getSqlSession().insert("com.test.vo.user.mapper.add", relatedId);
    }
}

通过如图的this.getSqlSession.*这个语句就可以做相关的调用了,而前面的注解@Repository("relatedIdDao")就是自动装配的注解 @Autowired是自动装配时候所找的函数位置,而setSqlSessionFactory(SqlSessionFactory sqlSessionFactory)这个函数是必要的。

最后是相关jar包,这个jar包数量可能比需要的多,因为这是后来整理的。

aopalliance.jar
asm-3.3.1.jar
aspectjweaver.jar
cglib-2.2.2.jar
commons-logging-1.1.1.jar
javassist-3.17.1-GA.jar
log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
mybatis-3.2.7.jar
mybatis-spring-1.2.1.jar
mysql-connector-java-5.1.20-bin.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
spring-aop-4.1.6.RELEASE.jar
spring-aspects-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-context-support-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-jdbc-4.1.6.RELEASE.jar
spring-orm-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar
spring-web-4.1.6.RELEASE.jar
spring-webmvc-4.1.6.RELEASE.jar

大功告成!!!

  

原文地址:https://www.cnblogs.com/K-artorias/p/6777594.html