搭建项目框架
第一步:创建Maven项目
- 添加依赖
junit
junit
4.12
test
org.projectlombok
lombok
1.18.10
mysql
mysql-connector-java
8.0.11
org.mybatis
mybatis
3.5.4
- 指定映射文件位置
src/main/java
**/*.xml
创建MySQL配置文件mysql.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1/test?useSSL=false&serverTimezone=CST
username=root
password=root
第三步:创建MyBatis配置文件mybatis-cfg.xml
第四步:创建MyBatis工具类
public class MyBatisUtil {
private static String CONFIG_FILE_LOCATION = "mybatis-cfg.xml";
private static final ThreadLocal threadLocal = new
ThreadLocal();
private static SqlSessionFactory sessionFactory;
static {
try {
Reader reader=Resources.getResourceAsReader(CONFIG_FILE_LOCATION);
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
sessionFactory=builder.build(reader);
} catch (Exception e) {
e.printStackTrace();
}
}
public static SqlSession getSession() {
SqlSession session = threadLocal.get();
if (session == null) {
session = (sessionFactory!=null) ? sessionFactory.openSession() : null;
threadLocal.set(session);
}
return session;
}
public static void closeSession() {
SqlSession session = threadLocal.get();
if (session != null) {
session.close();
threadLocal.set(null);
}
}
}
具体代码
第一步:创建实体类
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ToString(callSuper = true)
public class Dept {
/**
* 部门编号
*/
private Byte deptno;
/**
* 部门名称
*/
private String dname;
/**
* 部门地址
*/
private String loc;
}
第二步:创建DeptDao.java
public interface DeptDao {
int deleteByPrimaryKey(Byte deptno);
int insert(Dept record);
int insertSelective(Dept record);
Dept selectByPrimaryKey(Byte deptno);
int updateByPrimaryKeySelective(Dept record);
int updateByPrimaryKey(Dept record);
}
第三步:创建DeptDao.xml
deptno, dname, loc
select
from tb_dept
where deptno = #{deptno,jdbcType=TINYINT}
delete from tb_dept
where deptno = #{deptno,jdbcType=TINYINT}
insert into tb_dept (deptno, dname, loc )
values (#{deptno,jdbcType=TINYINT}, #{dname,jdbcType=VARCHAR}, #{loc,jdbcType=VARCHAR} )
insert into tb_dept
deptno,
dname,
loc,
#{deptno,jdbcType=TINYINT},
#{dname,jdbcType=VARCHAR},
#{loc,jdbcType=VARCHAR},
update tb_dept
dname = #{dname,jdbcType=VARCHAR},
loc = #{loc,jdbcType=VARCHAR},
where deptno = #{deptno,jdbcType=TINYINT}
update tb_dept
set dname = #{dname,jdbcType=VARCHAR},
loc = #{loc,jdbcType=VARCHAR}
where deptno = #{deptno,jdbcType=TINYINT}
第四步:测试代码
public class DeptMapperTest {
private SqlSession session;
private DeptDao mapper;
@Before
public void before(){
session = MyBatisUtil.getSession();
mapper = session.getMapper(DeptDao.class);
}
@After
public void after(){
session.commit();
}
@Test
public void deleteByPrimaryKey() {
int res = mapper.deleteByPrimaryKey((byte) 46);
System.out.println(res);
}
@Test
public void insert() {
Dept dept = new Dept((byte)1,"aa","aaaaaaaaaaa");
int res = mapper.insert(dept);
System.out.println(res);
}
@Test
public void insertSelective() {
Dept dept = new Dept();
dept.setDname("abcd");
int res = mapper.insertSelective(dept);
System.out.println(res);
}
@Test
public void selectByPrimaryKey() {
Dept dept = mapper.selectByPrimaryKey((byte) 10);
System.out.println(dept);
}
@Test
public void updateByPrimaryKeySelective() {
Dept dept = new Dept();
dept.setDeptno((byte)40);
dept.setDname("abc");
int res = mapper.updateByPrimaryKeySelective(dept);
System.out.println(res);
}
@Test
public void updateByPrimaryKey() {
// Dept dept = new Dept();
// dept.setDeptno((byte)47);
// dept.setDname("asdf");
// int res = mapper.updateByPrimaryKey(dept);
System.out.println(3/0);
}
}