重新搭建一个新的MyBatis环境,进行MyBatis的CRUD测试;
package com.dfbz.dao;
/**
* @author lscl
* @version 1.0
* @intro:
*/
import com.dfbz.entity.Emp;
public interface EmpDao {
void save(Emp emp);
}
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dfbz.dao.EmpDao">
<insert id="save" parameterType="com.dfbz.entity.Emp">
insert into emp values(null,#{name},#{age},#{addr},#{salary})
insert>
mapper>
package com.dfbz.test;
import com.dfbz.dao.EmpDao;
import com.dfbz.entity.Emp;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
public class Demo01 {
@Test
public void test1() throws IOException {
//获取主配置文件流
InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
//获取工厂构造器对象
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//获取session工厂
SqlSessionFactory factory = builder.build(is);
//获取session会话对象,与数据交互会话
/*
true: 自动提交
false: 手动提交(默认值)
*/
SqlSession session = factory.openSession(true); // 是否开启自动提交,默认值:false
EmpDao empDao = session.getMapper(EmpDao.class);
// 封装数据
Emp emp = new Emp(null,"小兰",20,"湖北襄阳",5000.0);
// 执行保持
empDao.save(emp);
//释放资源
session.close();
is.close();
}
}
Emp emp = new Emp(null, "小红", 18, "江西抚州", 7000.0);
System.out.println("保存之前: " + emp);
empDao.save(emp);
System.out.println("保存之后前: " + emp);
执行结果:
数据库查询自增长主键的值
在dao接口中扩展一个方法:
void save2(Emp emp);
dao接口映射:
<insert id="save2" parameterType="com.dfbz.entity.Emp">
<selectKey keyColumn="id" keyProperty="id" resultType="Integer" order="AFTER">
select LAST_INSERT_ID()
selectKey>
insert into emp values(null,#{name},#{age},#{addr},#{salary})
insert>
测试:
适合支持主键自增长的数据库
void save3(Emp emp);
<insert id="save3" parameterType="com.dfbz.entity.Emp" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
insert into emp values(null,#{name},#{age},#{addr},#{salary})
insert>
void update(Emp emp);
<update id="update" parameterType="com.dfbz.entity.Emp">
update emp set name=#{name},age=#{age},addr=#{addr},salary=#{salary} where id=#{id}
update>
private SqlSession session = null;
private EmpDao empDao = null;
@Before
public void before() throws Exception {
// 1. 创建Session工厂的构建对象
SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
// 2. 通过工厂构建对象来创建一个Session工厂
SqlSessionFactory sessionFactory = factoryBuilder.build(Resources.getResourceAsStream("MyBatisConfig.xml"));
// 3. 通过session工厂来创建session
session = sessionFactory.openSession();
// 4. 通过session获取Mapper接口
empDao = session.getMapper(EmpDao.class);
}
@After
public void after() throws Exception {
session.close();
}
@Test
public void test2() throws IOException {
// 封装数据
Emp emp = new Emp(1, "小灰", 22, "江西南昌", 5000.0);
// 执行修改
empDao.update(emp);
}
void delete(Integer id);
<delete id="delete" parameterType="java.lang.Integer">
delete from emp where id=#{id}
delete>
Emp findById(Integer id);
List<Emp> findAll();
<select id="findById" parameterType="java.lang.Integer" resultType="com.dfbz.entity.Emp">
select * from emp where id=#{id}
select>
<select id="findByNameLike" parameterType="java.lang.String" resultType="com.dfbz.entity.Emp">
select * from emp where name like #{name}
select>
@Test
public void test3() throws IOException {
// 根据id查询
Emp emp = empDao.findById(1);
System.out.println(emp);
}
@Test
public void test4() throws IOException {
List<Emp> empList = empDao.findAll();
for (Emp emp : empList) {
System.out.println(emp);
}
}
Long count();
<select id="count" resultType="java.lang.Long">
select count(1) from emp
select>
@Test
public void test5() throws IOException {
Long count = empDao.count();
System.out.println("查询到: " + count + "条记录");
}