Spring3集成Mybatis3

刚把Spring3与Mybatis3集成起,记录一下.本文只为了Spring与Mybatis集成,没有太复杂的应用,配置相对简单,如果有需求,可以根据自己需求添加更详细配置.

jar准备

MyBatis-3.1.1.jar
Mybatis-spring-1.1.1.jar
mysql-connector-java-5.1.1.21
Commons-dbcp-1.4.jar
Commons-pool-1.6.jar
Commons-logging-1.1.1.jar
org.springframework.beans-3.1.2.RELEASE.jar
org.springframework.core-3.1.2.RELEASE.jar
org.springframework.context-3.1.2.RELEASE.jar
org.springframework.tx-3.1.2.RELEASE.jar

1 编写实体

package com.ywxm.bean;

public class Type {

	private int id;
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

2 编写application.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ywxm
jdbc.username=root
jdbc.password=root
3编写Spring的配置文件applicationContext.xml

     <!-- 使用注解自动注入bean -->
	<context:component-scan base-package="com.ywxm" />
	<context:property-placeholder location="classpath*:/application.properties" />

	<!--数据源配置 -->

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<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="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:mybatis.xml" />
	    <property name="mapperLocations" value="classpath:mapper/*_mapper.xml" />			
	</bean>

        <!-- 事务管理 -->
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
     </bean>
     <tx:annotation-driven transaction-manager="transactionManager" />

4 编写Mybatis的配置文件 mybatis.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.ywxm.bean.Type" alias="type" />
	</typeAliases>

</configuration>
5 编写mapper.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="Type">

	<resultMap type="com.ywxm.bean.Type" id="Type">
		<id column="id" property="id" />
		<result column="name" property="name" />
	</resultMap>

	<insert id="save" parameterType="type">
		insert into
		type(id,name)values(#{id},#{name})
	</insert>
        
        <!-- ibatis 配置id=#id# ,mysqlbatis 配置id=#{id}-->
	<select id="get" parameterType="int" resultType="type">
		select * from type where id=#{id}
	</select>

</mapper>
6 Dao编写

package com.ywxm.dao;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ywxm.bean.Type;

@Component
public class TypeDao {
	

	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;

	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		this.sqlSessionTemplate = sqlSessionTemplate;
	}
	
	public Type get(int id){
		return sqlSessionTemplate.selectOne("Type.get", id);
	}
	
	

	public void save(Type type) {
		sqlSessionTemplate.insert(".save", type);
	}
}

  请注意上图中insert("sava",user) 中没有使用namespace值(mapper.xml中),那么必须保证所有mapper文件中"save"是唯一的.我就是犯了这种错误,结果就是数据库操作不成功,但不报错. 浪费了很多时间. 这让我感觉Mybatis测试有点困难.如果使用namespace值( 例:selectOne("Type.get", id))那么不同mapper 文件中有相同的"get" ,那么无所谓.
7 service编写

package com.ywxm.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.ywxm.bean.Type;
import com.ywxm.dao.TypeDao;

@Component
@Transactional
public class TypeManager {

	@Autowired
	private TypeDao type;

	public void setType(TypeDao type) {
		this.type = type;
	}

	public void save(Type type) {
		this.type.save(type);

	}
	public void get(int id){
	   Type t=type.get(id);
	   
	}

}


在上面配置中dao继承SqlSessionDaoSupport,如果不想使用继承,我们可以稍作改动在Spring的配置文件applicationContext.xml中增加

    <!--  配置sqlSessionTemplate -->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
	<constructor-arg index="0" ref="sqlSessionFactory" />
    </bean> 

在把dao修改如下即可

package com.ywxm.dao;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ywxm.bean.Type;

@Component
public class TypeDao {
	

	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;

	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		this.sqlSessionTemplate = sqlSessionTemplate;
	}
	
	public Type get(int id){
		return sqlSessionTemplate.selectOne("Type.get", id);
	}
	
	

	public void save(Type type) {
		sqlSessionTemplate.insert("Type.save", type);
	}
}

那为何可以这样呢,通过查看SqlSessionDaoSupport类 发现有这样一段话

" This class needs a SqlSessionTemplate or a SqlSessionFactory. If both are set the SqlSessionFactory will be ignored."

也就是需要SqlSessionTemplate或SqlSessionFactory. 看它源码 SqlSessionFactory作为参数传给SqlSessionTemplate.无论是SqlSessionTemplate还是SqlSessionFactory.最终都是获得SqlSessionTemplate实例.通过SqlSessionTemplate进行数据库操作.因此给它个SqlSessionTemplate实例.从而不使用SqlSessionDaoSupport使用SqlSessionTemplate也能成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值