方案一:使用绝对定位SQL的方式
手动编写DAO接口和实现类
namespace+id定位动态SQL
第一步:创建项目,导入依赖 mysql,mybatis,log4j,Spring
<meta charset="utf-8">

dbcp连接池依赖:

Mybatis的插件,资源拷贝,指定编译版本。
第二步:插件生成表对应的实体类和mapper映射文件
注意:mybatis的核心配置文件,可以整合到没有

注意:
不生成DAO接口(非Mapper代理方式)
不生成Example
需要手动编写DAO接口和实现类。
generatorConfig.xml 代码如下 :
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration >
<classPathEntry location="D:\Program Files\repository\mysql\mysql-connector-java\5.1.34\mysql-connector-java-5.1.34.jar" ></classPathEntry>
<context id="context1" >
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/testmybatis01?characterEncoding=utf-8" userId="root" password="zyh" />
<javaModelGenerator targetPackage="com.zyh.pojo" targetProject="src/main/java" />
<sqlMapGenerator targetPackage="com.zyh.mapper" targetProject="src/main/java" />
<table tableName="t_user_info" domainObjectName="UserInfo"
enableCountByExample="false"
enableUpdateByExample="false"
enableSelectByExample="false"
enableDeleteByExample="false">
</table>
</context>
</generatorConfiguration>
生成:只能点击一次。 使用mybatis 自动生成插件
注意:需要注意的地方如上配置文件所示: 取到了自动生成 dao 以及 不自动生成模板 example 。

第三步:开发DAO接口和实现类
需要继承整合类:extends org.mybatis.spring.support.SqlSessionDaoSupport
它可以帮助我们直接得到SqlSession
得到 SqlSession的方式,调用父类的方法getSqlSession()
this.getSqlSession()【需要注入: sqlSessionFactory】
不需要手动关闭,容器管理
注意:事务不要用SqlSession直接提交。
Dao 如下所示 :
public interface UserInfoDao {
void addUserInfo(UserInfo userInfo);
void deleteUserinfo(Long aLong);
void updateUserInfo(UserInfo userInfo);
UserInfo queryUserInfoById(Long aLong);
List<UserInfo> queryUserInfoAll();
}
DaoImpl如下所示:
public class UserInfoDaoImpl extends SqlSessionDaoSupport implements UserInfoDao {
@Override
public void addUserInfo(UserInfo userInfo) {
getSqlSession().insert("com.zyh.mapper.UserInfoMapper.insert",userInfo);
}
@Override
public void deleteUserinfo(Long aLong) {
getSqlSession().delete("com.zyh.mapper.UserInfoMapper.deleteByPrimaryKey",aLong);
}
@Override
public void updateUserInfo(UserInfo userInfo) {
getSqlSession().update("com.zyh.mapper.UserInfoMapper.updateByPrimaryKeySelective",userInfo);
}
@Override
public UserInfo queryUserInfoById(Long aLong) {
return getSqlSession().selectOne("com.zyh.mapper.UserInfoMapper.selectByPrimaryKey",aLong);
}
@Override
public List<UserInfo> queryUserInfoAll() {
return getSqlSession().selectList("com.zyh.mapper.UserInfoMapper.selectUserInfoAll");
}
}
通过源码分析:可以只需要注入 SqlSessionFactory ,就可以得到SqlSession
第四步:配置整合持久层
使用Spring的核心配置文件替换Mybatis的核心配置文件。
同时由Spring来提供SqlSessionFactory
部分配置文件如下所示:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver"></property>
<property name="url" value="jdbc:mysql:///testmybatis01?characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="zyh"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:com/zyh/mapper/*Mapper.xml"></property>
</bean>
<bean id="userInfoDao" class="com.zyh.dao.UserInfoDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
配置整合:
1)连接池
org.gjt.mm.mysql.Driver
jdbc:mysql:///testmybatis01?characterEncoding=utf-8

-
创建SqlSessionFactory(由Spring提供)
工厂org.mybatis.spring.SqlSessionFactoryBean
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:com/zyh/mapper/*Mapper.xml"></property>
</bean>
含有注册实体
源码确认:这个工厂 Bean返回的对象是:SqlSessionFactory

-
把工厂注入给DAO(让DAO具备得到SqlSession的能力)
<bean id="userInfoDao" class="com.zyh.dao.UserInfoDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
image.png
第五步 : 测试 :
增删改查如下所示: 都是成功的 输出结果就不给大家截图了。
public class testUserInfoDao {
@Test
public void testUserInfoDaoAdd(){
ClassPathXmlApplicationContext classPathXmlApplicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
UserInfoDao userInfoDao = (UserInfoDao) classPathXmlApplicationContext.getBean("userInfoDao");
UserInfo userInfo=new UserInfo();
userInfo.setuName("张颖豪8/16");
userInfo.setuPass("魏雪8/16");
userInfoDao.addUserInfo(userInfo);
}
@Test
public void testUserInfoDaoUpdate(){
ClassPathXmlApplicationContext classPathXmlApplicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
UserInfoDao userInfoDao = (UserInfoDao) classPathXmlApplicationContext.getBean("userInfoDao");
UserInfo userInfo=new UserInfo();
userInfo.setuId(138l);
userInfo.setuName("张颖豪xxx8/16");
userInfo.setuPass("魏雪xxx8/16");
userInfoDao.updateUserInfo(userInfo);
}
@Test
public void testFindUserInfoDao(){
ClassPathXmlApplicationContext classPathXmlApplicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
UserInfoDao userInfoDao = (UserInfoDao) classPathXmlApplicationContext.getBean("userInfoDao");
UserInfo userInfo = userInfoDao.queryUserInfoById(138l);
Log.info(userInfo);
}
@Test
public void testUserInfoDaoDelete(){
ClassPathXmlApplicationContext classPathXmlApplicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
UserInfoDao userInfoDao = (UserInfoDao) classPathXmlApplicationContext.getBean("userInfoDao");
userInfoDao.deleteUserinfo(138l);
}
@Test
public void testQueryAll(){
ClassPathXmlApplicationContext classPathXmlApplicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
UserInfoDao userInfoDao = (UserInfoDao) classPathXmlApplicationContext.getBean("userInfoDao");
List<UserInfo> userInfos = userInfoDao.queryUserInfoAll();
Log.info(userInfos);
}
}
AOP事务(由Spring提供)
不用自己编写事务通知类
一般是在服务层加AOP事务。不是在持久层加。

优化:

添加服务层,在服务层加AOP事务。
事务传递到持久层
使用Spring事务通知类整合
声明式事务
事务管理
传统方式:手动编码在业务逻辑层控制事务
采取AOP的方式实现声明式事务
顶级接口:(事务通知类)
org.springframework.transaction.PlatformTransactionManager
Mybatis操作数据库事务,使用Spring 的JDBC事务控制类。
实现类:
org.springframework.jdbc.datasource.DataSourceTransactionManager
第六步:开发服务层接口和实现类
public interface UserService {
void addUserInfo(UserInfo userInfo);
void deleteUserinfo(Long aLong);
void updateUserInfo(UserInfo userInfo);
UserInfo queryUserInfoById(Long aLong);
List<UserInfo> queryUserInfoAll();
void updateAll(UserInfo addUserInfo,UserInfo updateInfo);
}
实现类如下所示:
public class UserServiceImpl implements UserService {
private UserInfoDaoImpl userInfoDao;
public void setUserInfoDao(UserInfoDaoImpl userInfoDao) {
this.userInfoDao = userInfoDao;
}
@Override
public void addUserInfo(UserInfo userInfo) {
userInfoDao.addUserInfo(userInfo);
}
@Override
public void deleteUserinfo(Long aLong) {
userInfoDao.deleteUserinfo(aLong);
}
@Override
public void updateUserInfo(UserInfo userInfo) {
userInfoDao.updateUserInfo(userInfo);
}
@Override
public UserInfo queryUserInfoById(Long aLong) {
return userInfoDao.queryUserInfoById(aLong);
}
@Override
public List<UserInfo> queryUserInfoAll() {
return userInfoDao.queryUserInfoAll();
}
@Override
public void updateAll(UserInfo addUserInfo,UserInfo updateInfo){
userInfoDao.addUserInfo(addUserInfo);
userInfoDao.updateUserInfo(updateInfo);
}
}
第七步:配置整合AOP事务
思路:事务管理器->事务通知切面->通知者
事务顶级接口:

总的配置文件如下所示:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver"></property>
<property name="url" value="jdbc:mysql:///testmybatis01?characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="zyh"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:com/zyh/mapper/*Mapper.xml"></property>
</bean>
<bean id="userInfoDao" class="com.zyh.dao.UserInfoDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<!-- 4,创建服务层对象,注入DAO 目标类对象-->
<bean id="userInfoService" class="com.zyh.service.UserServiceImpl">
<property name="userInfoDao" ref="userInfoDao"></property>
</bean>
<!-- 5,创建事务管理通知类对象-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 6,定义事务通知切面,让事务应用到具体的方法 -->
<tx:advice id="myAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="query*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 7,AOP编程:让切入点和通知关联形成切面 -->
<aop:config>
<aop:pointcut id="myPoint" expression="execution(* com.zyh.service.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="myAdvice" pointcut-ref="myPoint"></aop:advisor>
</aop:config>
aop全自动编程:tx:method指向服务层的方法。
Mybatis有的版本底层可以自动提交事务,从服务层的角度,必须加上AOP事务配置。【我们这个版本没有这个问题】
第八步:测试
测试方法如下所示:
public class testUserInfoTransaction {
@Test
public void testtransaction(){
ClassPathXmlApplicationContext classPathXmlApplicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userInfoService = (UserService) classPathXmlApplicationContext.getBean("userInfoService");
UserInfo addUserInfo=new UserInfo();
addUserInfo.setuName("我是新添加的用户名");
addUserInfo.setuPass("我是新添加的密码");
UserInfo updateUserInfo=new UserInfo();
updateUserInfo.setuId(12l);
updateUserInfo.setuName("事物增加");
updateUserInfo.setuPass("事物增加");
userInfoService.updateAll(addUserInfo,updateUserInfo);
}
}
在实现类中的方法如下所示: 可以看到 第一条和第二条语句中间有个异常产生
当我们没有做事物处理的前后 得出的结果是完全不一致的。
@Override
public void updateAll(UserInfo addUserInfo,UserInfo updateInfo){
userInfoDao.addUserInfo(addUserInfo);
int i=1/0;
userInfoDao.updateUserInfo(updateInfo);
}
网友评论