搭建項目框架
第一步:創建Maven項目
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
創建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
<?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">
<configuration>
<properties resource="mysql.properties"></properties>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}"/>
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/hc/dao/mapping/DeptDao.xml"/>
</mappers>
</configuration>
第四步:創建MyBatis工具類
public class MyBatisUtil {
private static String CONFIG_FILE_LOCATION = "mybatis-cfg.xml";
private static final ThreadLocal<SqlSession> threadLocal = new
ThreadLocal<SqlSession>();
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
<?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">
<mapper namespace="com.hc.dao.DeptDao">
<resultMap id="BaseResultMap" type="com.hc.bean.Dept">
<id column="deptno" jdbcType="TINYINT" property="deptno"/>
<result column="dname" jdbcType="VARCHAR" property="dname"/>
<result column="loc" jdbcType="VARCHAR" property="loc"/>
</resultMap>
<sql id="Base_Column_List">
deptno, dname, loc
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Byte" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from tb_dept
where deptno = #{deptno,jdbcType=TINYINT}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Byte">
delete from tb_dept
where deptno = #{deptno,jdbcType=TINYINT}
</delete>
<insert id="insert" parameterType="com.hc.bean.Dept">
insert into tb_dept (deptno, dname, loc )
values (#{deptno,jdbcType=TINYINT}, #{dname,jdbcType=VARCHAR}, #{loc,jdbcType=VARCHAR} )
</insert>
<insert id="insertSelective" parameterType="com.hc.bean.Dept">
insert into tb_dept
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="deptno != null">
deptno,
</if>
<if test="dname != null">
dname,
</if>
<if test="loc != null">
loc,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="deptno != null">
#{deptno,jdbcType=TINYINT},
</if>
<if test="dname != null">
#{dname,jdbcType=VARCHAR},
</if>
<if test="loc != null">
#{loc,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.hc.bean.Dept">
update tb_dept
<set>
<if test="dname != null">
dname = #{dname,jdbcType=VARCHAR},
</if>
<if test="loc != null">
loc = #{loc,jdbcType=VARCHAR},
</if>
</set>
where deptno = #{deptno,jdbcType=TINYINT}
</update>
<update id="updateByPrimaryKey" parameterType="com.hc.bean.Dept">
update tb_dept
set dname = #{dname,jdbcType=VARCHAR},
loc = #{loc,jdbcType=VARCHAR}
where deptno = #{deptno,jdbcType=TINYINT}
</update>
</mapper>
第四步:測試代碼
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() {
System.out.println(3/0);
}
}