这篇文章主要讲解Springboot整合Mybatis实现一个最基本的增删改查功能,整合的方式有两种一种是注解形式的。
第一步:数据库新建Person表
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for person -- ---------------------------- DROP TABLE IF EXISTS `person`; CREATE TABLE `person` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, `age` int(11) DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; SET FOREIGN_KEY_CHECKS = 1;
这个表结构三个字段id、name、age,以id为主键且递增。
第二步:新建Springboot项目(版本供参考)
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.12.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
最终的目录结构:
第三步:导入相关依赖
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency>
第四步:更改application配置文件(配置自己的地址和密码,扫描的包)
#服务启动端口配置 server.port=80 #数据库连接信息配置 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8&useSSL=false #用户名 spring.datasource.username=root #密码 spring.datasource.password=123456 #驱动 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver mybatis.type-aliases-package=com.minos.onestepdemo01.dao;
第五步:新建dao包,在dao包下新建Person类
package com.minos.onestepdemo01.dao; public class Person { private Integer id; private String name; private Integer age; public Person() { } public Person(Integer id, String name, Integer age) { this.id = id; this.name = name; this.age = age; } 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; } @Override public String toString() { return "Person{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
这个类和数据库中的Person类一一对应的。
第六步:新建mapper包,在mapper新建PersonMapper类
package com.minos.onestepdemo01.mapper; import com.minos.onestepdemo01.dao.Person; import org.apache.ibatis.annotations.*; import java.util.List; @Mapper public interface PersonMapper { // 增加一个Person @Insert("insert into person(id,name,age)values(#{id},#{name},#{age})") int insert(Person person); // 删除一个Person @Delete("delete from person where id=#{id}") int deleteByPrimaryKey(Integer id); // 更改一个Person @Update("update person set name=#{name},age=#{age} where id=#{id}") int updateByPrimaryKey(Person person); // 查询一个Person @Select("select id,name,age from person where id=#{id}") Person selectByPrimaryKey(Integer id); // 查询所有Person @Select("select id,name,age from person") List<Person> selectAllPerson(); }
第七步:新建service包,在service包创建PersonService接口
package com.minos.onestepdemo01.service; import com.minos.onestepdemo01.dao.Person; import java.util.List; public interface PersonService { // 增加一个Person int insertPerson(Person person); // 删除一个Person int deleteByPersonId(Integer id); // 更改一个Person int updateByPersonId(Person person); // 查询一个Person Person selectByPersonId(Integer id); // 查询所有的Person List<Person> selectAllPerson(); }
第八步:在service包下创建PersonServiceImpl接口实现类
package com.minos.onestepdemo01.service; import com.minos.onestepdemo01.dao.Person; import com.minos.onestepdemo01.mapper.PersonMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class PersonServiceImpl implements PersonService{ @Autowired private PersonMapper personMapper; @Override public int insertPerson(Person person) { return personMapper.insert(person); } @Override public int deleteByPersonId(Integer id) { return personMapper.deleteByPrimaryKey(id); } @Override public int updateByPersonId(Person person) { return personMapper.updateByPrimaryKey(person); } @Override public Person selectByPersonId(Integer id) { return personMapper.selectByPrimaryKey(id); } @Override public List<Person> selectAllPerson() { return personMapper.selectAllPerson(); } }
第九步:编写controller层
package com.minos.onestepdemo01.controller; import com.minos.onestepdemo01.dao.Person; import com.minos.onestepdemo01.service.PersonService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class PersonController { @Autowired private PersonService personService; @RequestMapping(value = "/add") public String students(){ Person person=new Person(); person.setId(1); person.setName("java"); person.setAge(18); int result=personService.insertPerson(person); System.out.println("插入的结果是"+result); return result+""; } @GetMapping("/findAll") public String findall(){ return personService.selectAllPerson().toString()+""; } }
第十步:在启动类添加扫描器
package com.minos.onestepdemo01; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.minos.onestepdemo01.mapper") public class Onestepdemo01Application { public static void main(String[] args) { SpringApplication.run(Onestepdemo01Application.class, args); } }
第十一步:测试
在浏览器输入相应的路径即可。
1
不要重复加载add(如果修改了PersonController内person.setId(数字);内的数字可以),数据表中的id是主键,第二次加载
Whitelabel Error Page
[Person{id=1, name='java', age=18}]