java入門016~springboot2結合mybatis,免xml配置

我們上一節給大家講了springboot2結合mybatis實現mysql數據的增刪改查,但是是要用到xml配置的,一旦涉及到xml配置,就會比較麻煩。今天再來給大家講一個新的方法,不用設置xml文件,並且代碼看起來更簡潔。

一,引入mybatis和數據庫連接的依賴


完整的pom.xml貼出來給大家

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.shitou</groupId>
    <artifactId>mybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mybatis</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--        mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>LATEST</version>
        </dependency>
        <!--        數據庫鏈接依賴-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

二,配置數據庫連接


這裏我的數據庫是用的mysql數據庫,使用的是test2庫。

三,建庫建表

創建test2數據庫

create database test2 default character set utf8 collate utf8_general_ci;

創建user2數據表

CREATE TABLE user2(
    id   int(32) auto_increment   NOT NULL PRIMARY KEY,
    name VARCHAR(32) comment '姓名' NOT NULL,
    age  VARCHAR(32) comment '年紀' NOT NULL
) COMMENT '用戶表' CHARACTER SET utf8 COLLATE utf8_general_ci;

四,創建與數據表對應的實體類

五,創建操作數據庫的mapper


代碼給大家貼出來

@Mapper
public interface User2Mapper {
    @Insert("insert into user2(name, age) values(#{name}, #{age})")
    int add(@Param("name") String name, @Param("age") int age);

    @Update("update user2 set name = #{name}, age = #{age} where id = #{id}")
    int update(@Param("name") String name, @Param("age") int age, @Param("id") int id);

    @Delete("delete from user2 where id = #{id}")
    int delete(int id);

    @Select("select id, name as name, age as age from user2 where id = #{id}")
    User2 findOne(@Param("id") int id);

    @Select("select * from user2")
    List<User2> findAll();
}

六,創建service和controller

service代碼如下

@Service
public class User2Service {

    @Resource
    private User2Mapper accountMapper;

    public int add(String name, int age) {
        return accountMapper.add(name, age);
    }

    public int update(String name, int age, int id) {
        return accountMapper.update(name, age, id);
    }

    public int delete(int id) {
        return accountMapper.delete(id);
    }

    public User2 findAccount(int id) {
        return accountMapper.findOne(id);
    }

    public List<User2> findAccountList() {
        return accountMapper.findAll();
    }
}

controller代碼如下

@RestController
@RequestMapping("/mybatis")
public class User2Controller {

    @Resource
    User2Service accountService;

    @GetMapping("/list")
    public List<User2> getAccounts() {
        return accountService.findAccountList();
    }

    @GetMapping("/findone")
    public User2 getAccountById(@RequestParam("id") int id) {
        return accountService.findAccount(id);
    }

    @GetMapping("/update")
    public String updateAccount(@RequestParam("id") int id,
                                @RequestParam(value = "name") String name,
                                @RequestParam(value = "age") int age) {
        int t = accountService.update(name, age, id);
        if (t == 1) {
            return "success";
        } else {
            return "fail";
        }

    }

    @GetMapping("/delete")
    public String delete(@RequestParam(value = "id") int id) {
        int t = accountService.delete(id);
        if (t == 1) {
            return "success";
        } else {
            return "fail";
        }

    }

    @GetMapping("/add")
    public String postAccount(@RequestParam(value = "name") String name,
                              @RequestParam(value = "age") int age) {
        int t = accountService.add(name, age);
        if (t == 1) {
            return "success";
        } else {
            return "fail";
        }
    }
}

七,啓動項目,做驗證

啓動springboot項目

添加數據。如下圖,我們通過add添加兩個數據

通過list 查詢所有數據

更新和刪除數據,就不給大家演示了。

源碼:

https://github.com/qiushi123/springboot-demos

視頻講解

https://edu.csdn.net/course/detail/23443

往期回顧

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章