Springboot整合mybatis

xiaoxiao2021-02-28  27

Springboot整合mybatis

1.在pom中加入所需要的依赖:

<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.spring.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>

2.在application.yml中配置数据源和mybatis的相关信息

#数据源 spring: datasource: url: jdbc:mysql://localhost:3306/cfcc_pt username: root password: root driver-class-name: com.mysql.jdbc.Driver #mybatis mybatis: typeAliasesPackage: com.cfcc.pojo mapperLocations: classpath:mybatis/*.xml

3.在启动类的上面加上mapper扫描器,扫描所引用的包。

@SpringBootApplication @RestController @MapperScan("com.cfcc.mapper") public class CfccApplicaton { public static void main(String[] args) { SpringApplication.run(CfccApplicaton.class, args); } }

4.在数据库中新建一张user表

CREATE TABLE `cf_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `age` int(11) DEFAULT NULL, `addr` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

5.新建一个与用户对应的实体类

package com.cfcc.pojo; public class CfUser { private int id; private String name; private int age; private String addr; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } }

6.在resources下新建mybatis目录并在下面新建一个userMapper.xml。

在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.cfcc.mapper.UserMapper" > <select id="findUserById" parameterType="int" resultType="cfUser"> SELECT * from cf_user where id=#{id} </select> <select id="findUserListByName" resultType="cfUser"> SELECT * from cf_user </select> <!-- 插入并返回自增主键 --> <insert id="insertUser" parameterType="cfUser" useGeneratedKeys="true" keyProperty="id"> insert INTO cf_user(name, age, addr) VALUES (#{name},#{age},#{addr}) </insert> <update id="updateUser" parameterType="cfUser"> UPDATE cf_user set name=#{name},age=#{age},addr=#{addr} where id=#{id} </update> <delete id="deleteUserById" parameterType="int"> delete from cf_user where id=#{id} </delete> </mapper>

7.新建UserMapper接口,并编写对应的方法。

package com.cfcc.mapper; import com.cfcc.pojo.CfUser; import java.util.List; public interface UserMapper { CfUser findUserById(int id); List<CfUser> findUserListByName(); void insertUser(CfUser cfUser); void updateUser(CfUser cfUser); void deleteUserById(int id); }

8.在controller中编写方法调用mapper,使用简单的restful风格的url。

package com.cfcc.controller; import com.cfcc.mapper.UserMapper; import com.cfcc.pojo.CfUser; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("/rest") public class UserRestController { @Autowired private UserMapper mapper; @RequestMapping(value = "/", method = RequestMethod.GET) public List<CfUser> getUserList(){ List<CfUser> list = mapper.findUserListByName(); return list; } @RequestMapping(value = "/{id}", method = RequestMethod.GET) public CfUser getUserById(@PathVariable int id){ CfUser user = mapper.findUserById(id); return user; } @RequestMapping(value = "/", method = RequestMethod.POST) public CfUser postUser(){ CfUser user = new CfUser(); user.setName("张三"); user.setAge(23); user.setAddr("北京"); mapper.insertUser(user); return user; } @RequestMapping(value = "/{id}", method = RequestMethod.PUT) public String putUser(@PathVariable int id){ CfUser user = new CfUser(); user.setName("王五"); user.setAge(24); user.setAddr("广州"); user.setId(id); mapper.updateUser(user); return "put success"; } @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) public String deleteUser(@PathVariable int id){ mapper.deleteUserById(id); return "delete success"; } }

启动工程,即可在浏览器中访问对应的url。

转载请注明原文地址: https://www.6miu.com/read-2600311.html

最新回复(0)