使用Dao层+映射文件的方式实现增删改查,映射文件就相当于是Dao层的实现类。
MyBatis配置文件
只需要在上次的操作上稍作修改即可。
这次我们使用Log4j日志输出,在配置文件设置setting节点节点,修改后如下:
<?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">
<!-- MyBatis配置文件 -->
<configuration>
<!-- 配置log4j日志文件 -->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!-- 配置mybatis的多套运行环境 development为开发者模式 -->
<environments default="development">
<environment id="development">
<!--事务管理器 使用JDBC事务管理-->
<transactionManager type="JDBC"></transactionManager>
<!--dataSource 数据源配置 POOLED:MyBatis自带数据源 JNDI:基于tomcat的数据源 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///jdbcdemo20190804"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- 将mapper文件加入到配置文件中 -->
<mappers>
<mapper resource="mapper/EmpMapper.xml"/>
</mappers>
</configuration>
Dao层
然后新建Dao接口,Dao接口中的方法名要与映射文件中的Sql操作节点的id值一模一样
如下:
package com.xyj.dao;
import java.util.List;
import com.xyj.entity.Emp;
public interface EmpDao {
//方法名要与映射文件Sql操作的id值一模一样
List<Emp> select();
Emp selectById(int id);
int selectValue();//查询总记录数
int insert(Emp emp);
int update(Emp emp);
int delete(int id);
}
映射文件
让映射文件和我们的Dao层建立关联,只需要修改根节点mapper的namespace属性即可。
<mapper namespace="com.xyj.dao.EmpDao">
<?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">
<!-- 映射文件的根元素节点,namespace用于区分不同的mapper,全局唯一 -->
<mapper namespace="com.xyj.dao.EmpDao">
<!-- 查询数据表全部数据 id唯一标识符 resultType表示Sql语句返回值类型-->
<select id="select" resultType="com.xyj.entity.Emp">
select * from emp
</select>
<select id="selectById" resultType="com.xyj.entity.Emp">
select * from emp where eid=#{eid}
</select>
<select id="selectValue" resultType="int">
select count(1) from emp
</select>
<insert id="insert">
insert into emp values(default,#{ename},#{epwd},#{money})
</insert>
<update id="update">
update emp set ename=#{ename},epwd=#{epwd},money=#{money} where eid=#{eid}
</update>
<delete id="delete">
delete from emp where eid=#{eid}
</delete>
</mapper>
工具类
SqlSessionFactory对象一旦创建,就会在整个应用运行过程中始终存在。没有理由去销毁或再创建它,并且在应用运行中也不建议多次创建SqlSessionFactory。因此需要把代码进行优化,把原先测试类中的一些代码提取为工具类。
如下:
package com.xyj.utils;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
/**
* MyBatis工具类
*/
public class MyBatisUtils {
private static InputStream is;
private static SqlSessionFactory sessionFactory;
static {
try {
//加载MyBatis配置文件
is=Resources.getResourceAsStream("mybatis-config.xml");
//创建SqlSessionFactory对象来加工SqlSession(Sql语句执行对象)
sessionFactory=new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取Sql语句执行对象SqlSession
* @return
*/
public static SqlSession getSqlSession() {
return sessionFactory.openSession();
}
}
测试
实体类不变,测试如下:
package com.xyj.test;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.xyj.dao.EmpDao;
import com.xyj.entity.Emp;
import com.xyj.utils.MyBatisUtils;
/**
* 测试类
*/
public class TestMyBatis {
@org.junit.Test
public void test01() {
SqlSession session=null;
try {
//创建SqlSession Sql语句执行对象
session=MyBatisUtils.getSqlSession();
//执行操作
EmpDao ed = session.getMapper(EmpDao.class);
Emp emp = ed.selectById(13);
System.out.println(emp);
int i = ed.insert(new Emp("李联酋","234tgfwr",122));
System.out.println(i>0?"新增成功":"新增失败");
//手动提交事务
session.commit();
List<Emp> list = ed.select();
for (Emp emp2 : list) {
System.out.println(emp2);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//关闭SqlSession
session.close();
//事务回滚
//session.rollback();
}
}
}