mysql|springboot整合mybatis注解版

前言 昨天把springboot整合mybatis的xml版写了下,今天就写下注解版的。注解版更倾向于简单的sql语句,过于复杂的也能写,但是不推荐就是了。
xml格式的连接
配置
application.properties配置文件

spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/yu?useUnicode=true&characterEncoding=utf-8&&serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=yu123467.

测试用表建表语句
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

Mapper开发
package com.xue.mybitsdemo.dao; import com.xue.mybitsdemo.entity.StudentEntity; import org.apache.ibatis.annotations.*; import java.util.List; public interface StudentMapper { //根据id获取一条数据 @Select("select s_id,s_name,s_age,s_sex from student where s_id = #{id}") @Results({ @Result(column = "s_id", property = "id"), @Result(column = "s_name", property = "name"), @Result(column = "s_age", property = "age"), @Result(column = "s_sex", property = "sex"), }) StudentEntity getOne(@Param("id") Integer id); //根据多条件获取一条数据 @Select("select s_id,s_name,s_age,s_sex from student where s_id = #{id} and s_name =#{name}") @Results({ @Result(column = "s_id", property = "id"), @Result(column = "s_name", property = "name"), @Result(column = "s_age", property = "age"), @Result(column = "s_sex", property = "sex"), }) StudentEntity getOne2(StudentEntity studentEntity); //获取全部数据 @Select("select s_id,s_name,s_age,s_sex from student") @Results({ @Result(column = "s_id", property = "id"), @Result(column = "s_name", property = "name"), @Result(column = "s_age", property = "age"), @Result(column = "s_sex", property = "sex"), }) List> getAll(); //插入一条数据 @Insert("insert into student (s_name,s_age,s_sex) values (#{name},#{age},#{sex})") void insert(StudentEntity studentEntity); //删除一条数据 @Delete("delete fromstudent where s_id = #{id}") void deleteOne(@Param("id") Integer id); //修改一条数据 @Update("update student set s_name = #{name},s_age = #{age},s_sex = #{sex} where s_id=#{id}") void update(StudentEntity studentEntity); }

这里便是和xml的区别所在,相当于把xml中写的映射文件放在mapper中了。一些逻辑判断我就没写,如果写那个就会让sql语句看起来特别复杂,如果又需要自己去看看标签的用法。过于复杂的不推荐用注解!!!用xml清晰明了,为什么还要用注解呢?xml它不香么?
可以在文件上添加@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 + '\'' + '}'; } }

测试
package com.xue.mybitsdemo.Test; import com.xue.mybitsdemo.MybitsdemoApplication; import com.xue.mybitsdemo.dao.StudentMapper; import com.xue.mybitsdemo.entity.StudentEntity; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.List; @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> 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", 25, "female"); 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()); } }

【mysql|springboot整合mybatis注解版】如果配置过程中有什么问题,下面留言即可,我看到了会回复的。

    推荐阅读