mybatis开发过程中用到的还是比较多的,之前我用的大多都是注解形式,xml用的不多,所以就用了点时间吧xml格式的也看了下,做了总结在这里写下。
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
mysql
mysql-connector-java
#驱动名称
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#url地址
spring.datasource.url=jdbc:mysql://localhost:3306/poetry?useUnicode=true&characterEncoding=utf-8&&serverTimezone=GMT%2B8
#用户名
spring.datasource.username=root
#密码
spring.datasource.password=yu123467.
#扫描实体类路径
mybatis.type-aliases-package=com.xue.mybitsdemo.entity
#映射文件路径及名称
mybatis.mapper-locations:classpath:mapper/*Mapper.xml
#mybats配置文件
mybatis.config-location:classpath:config/mybits-config.xml
CREATE TABLE `student` (
`s_id` INT(11) NOT NULL AUTO_INCREMENT COMMENT '主键id',
`s_name` VARCHAR(50) NOT NULL DEFAULT '0' COMMENT '学生姓名',
`s_age` INT(11) NOT NULL DEFAULT '0' COMMENT '学生年龄',
`s_sex` VARCHAR(50) NOT NULL DEFAULT '0' COMMENT '学生性别',
PRIMARY KEY (`s_id`)
)
COMMENT='学生信息表'
ENGINE=InnoDB
;
-- main
--java
--com.xue.mybitsdemo
--dao
--StudentMapper.class
--entity
--StudentEntity.class
--MybitsdemoApplication.class
--resource
--config
mybatis-config.xml
--mapper
StudentMapper.xml
--application.properties
可设置项有很多,更多的戳这里
package com.xue.mybitsdemo.dao;
import com.xue.mybitsdemo.entity.StudentEntity;
import java.util.List;
public interface StudentMapper {
//根据id获取一条数据
StudentEntity getOne(Integer id);
//根据多条件获取一条数据
StudentEntity getOne2(StudentEntity studentEntity);
//获取全部数据
List<StudentEntity> getAll();
//插入一条数据
void insert(StudentEntity studentEntity);
//删除一条数据
void deleteOne(Integer id);
//修改一条数据
void update(StudentEntity studentEntity);
}
这些接口需要在映射文件中一一实现,可以在类文件上添加@Mapper注解,很多Mapper的话有点麻烦,推荐直接在启动类那添加。启动的时候,就去扫描,自动注入了
@SpringBootApplication
@MapperScan(basePackages= {"com.xue.mybitsdemo.dao"})
public class MybitsdemoApplication {
public static void main(String[] args) {
SpringApplication.run(MybitsdemoApplication.class, args);
}
}
package com.xue.mybitsdemo.entity;
public class StudentEntity {
private Integer id;
private String name;
private Integer age;
private String sex;
public StudentEntity(Integer id, String name, Integer age, String sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public StudentEntity(String name, Integer age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "StudentEntity{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
}
<?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.xue.mybitsdemo.dao.StudentMapper">
<resultMap id="studentMap" type="StudentEntity">
<id column="s_id" property="id" jdbcType="BIGINT"/>
<result column="s_name" property="name"/>
<result column="s_age" property="age"/>
<result column="s_sex" property="sex"/>
</resultMap>
<!-- <insert id="insert" useGeneratedKeys="true"-->
<!-- keyProperty="id" parameterType="StudentEntity" >-->
<!-- insert into student (s_name,s_age,s_sex)-->
<!-- values (#{name},#{age},#{sex})-->
<!-- </insert>-->
<insert id="insert" parameterType="StudentEntity">
insert into student (s_name,s_age,s_sex)
values (#{name},#{age},#{sex})
</insert>
<select id="getAll" resultMap="studentMap">
select s_id,s_name,s_age,s_sex from student
</select>
<select id="getOne" parameterType="Integer" resultMap="studentMap">
select s_id,s_name,s_age,s_sex from student where s_id = #{id}
</select>
<select id="getOne2" parameterType="StudentEntity" resultMap="studentMap">
select s_id,s_name,s_age,s_sex from student
<where>
<if test="id != null">
s_id=#{id}
</if>
<if test="name != null">
AND s_name=#{name}
</if>
<if test="age != null">
AND s_age=#{age}
</if>
<if test="sex != null">
AND s_sex=#{sex}
</if>
</where>
</select>
<delete id="deleteOne" parameterType="Integer">
delete from student where s_id = #{id}
</delete>
<!-- <update id="update" parameterType="StudentEntity">-->
<!-- update student set s_name = #{name},s_age = #{age},s_sex = #{sex} where s_id=#{id}-->
<!-- </update>-->
<update id="update" parameterType="StudentEntity">
update student
<set>
<if test="name != null">s_name=#{name},</if>
<if test="age != null">s_age=#{age},</if>
<if test="sex != null">s_sex=#{sex}</if>
</set>
where s_id=#{id}
</update>
</mapper>
resultMap 自定义返回结果。
主要用于转换字段,当数据库的字段和实体类的不一致的时候。其中column属性是数据库字段,property属性是实体类的字段。数据库里面的是“s_name”字段,实体类的是“name”字段。当然了,还有更简单的,那就是数据库查询的时候直接 select s_name as name from student。 用resultMap 主要是为了规范
parameterType 参数类型
namespace命名空间,可以通过此路径来调用映射文件。
update,getOne2两个方法,分别用了
udpate student where column = “value”,所以他们只是简化了非空的逻辑判断的sql语句组合写法,比如不用考虑是不是多个",",还有update字段的时候,是不是多个“AND”,这些mybtis都做好了,直接用就可以了
更多的戳这里
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MybitsdemoApplication.class)
public class StudentMapperTest {
@Autowired
private StudentMapper studentMapper;
@Test
public void insertTest() {
//添加数据
StudentEntity studentEntity = new StudentEntity(1, "yu", 24, "male");
StudentEntity studentEntity1 = new StudentEntity(2, "xue", 25, "female");
studentMapper.insert(studentEntity);
studentMapper.insert(studentEntity1);
Assert.assertEquals(2, studentMapper.getAll().size());
//获取全部数据
List<StudentEntity> studentEntityList = studentMapper.getAll();
for (StudentEntity studentEntitytemp : studentEntityList) {
System.out.println(studentEntitytemp.toString());
}
//根据id获取数据
StudentEntity studentEntity2 = studentMapper.getOne(2);
System.out.println(studentEntity2.toString());
Assert.assertEquals("xue", studentEntity2.getName());
//update部分数据项
StudentEntity studentEntity3 = new StudentEntity(2, "xueshanshan", null, null);
studentMapper.update(studentEntity3);
//根据实体类获取一条数据
StudentEntity studentEntity4 = studentMapper.getOne2(studentEntity3);
System.out.println(studentEntity4.toString());
Assert.assertEquals("xueshanshan", studentEntity4.getName());
//删除数据
studentMapper.deleteOne(2);
Assert.assertEquals(1, studentMapper.getAll().size());
}
}
xml更多的还是适用于复杂的sql语句,各种嵌套查询等,这些返回结果集中也都有相对应的处理,注解形式的,相较于xml而言,就简单多了,等我有空了再写那个。
如果配置过程中有什么问题,下面留言即可,我看到了会回复的。