mybatis与spring整合(方式一)
下面这种是使用了SqlSessionTemplate来实现
项目整体上只是在方式一上做了改动(实现方式变了),其余只有较小的调整
1、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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.cwy"/>
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}" />
<property name="jdbcUrl" value="${jdbcUrl}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${password}" />
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<property name="autoCommitOnClose" value="false"/>
<property name="checkoutTimeout" value="10000"/>
<property name="acquireRetryAttempts" value="2"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.cwy.entity"/>
<!-- 只是为了演示,因为没有更改默认配置,所以加不加无所谓
<property name="configLocation" value="classpath:mybatis-config.xml"/>
-->
<property name="mapperLocations" value="classpath:com/cwy/dao/mapper/*.xml"/>
</bean>
<!-- 与方式一的不同点-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory" />
</bean>
</beans>
jdbc.properties
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://127.0.0.1:3306/study?useUnicode=true&characterEncoding=utf-8
jdbc.username=cwy
password=151818
2、增加BseDao基类
package com.cwy;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
/**
* Created by asus on 2017/7/13.
*/
public class BaseDao {
@Autowired private SqlSessionTemplate template;
public BaseDao() {
System.out.println(template);
}
public <T> T findOne(String sqlId, Object params){
return template.selectOne(sqlId,params);
}
}
3、将UserDao接口改为实现类
package com.cwy.dao;
import com.cwy.BaseDao;
import com.cwy.entity.User;
import org.springframework.stereotype.Repository;
@Repository
public class UserDao extends BaseDao{
/**
* mybatis的mapper映射文件命名空间
*/
private static final String MAPPER_NAMESPACE="com.cwy.dao.UserDao";
public User getById(Long id){
return findOne(MAPPER_NAMESPACE+".queryById",id);//注意这儿的“.”!!
}
}
UserDao.xml(不变)
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cwy.dao.UserDao">
<resultMap id="BaseResultMap" type="User">
<id column="id" property="id" jdbcType="BIGINT" />
<result column="name" property="name" jdbcType="VARCHAR" />
</resultMap>
<select id="queryById" resultType="User" resultMap="BaseResultMap" parameterType="java.lang.Long">
SELECT *
FROM user
WHERE id=#{id}
</select>
</mapper>
项目整体结构
4、遇到的bug
注意加点和命名空间
template.selectOne(sqlId,params);
的sqld的写法为
findOne(MAPPER_NAMESPACE+".queryById",id);
注意两点:
1、MAPPER_NAMESPACE就是mapper的xml文件中的<mapper namespace="com.cwy.dao.UserDao">
2、是.queryById
而不是queryById
,否则会报IllegalArgumentException
jdbc.properties的书写顺序
写成这样的时候一直报错
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://127.0.0.1:3306/study?useUnicode=true&characterEncoding=utf-8
password=151818
jdbc.username=cwy
错误信息
java.sql.SQLException: Connections could not be acquired from the underlying database!
后来把用户名提前至密码前即可!
即
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://127.0.0.1:3306/study?useUnicode=true&characterEncoding=utf-8
jdbc.username=cwy
password=151818