1. 程式人生 > >SpringBoot--04.SpringBoot2.0整合mybatis

SpringBoot--04.SpringBoot2.0整合mybatis

開發環境:jdk1.8 tomcat8.5  mybatis springboot2.0 maven工程

專案結構:

1、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>

    <!--springBoot父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
    </parent>


    <groupId>com.day01springBoot</groupId>
    <artifactId>SpringBoot01</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <!--jdk版本-->
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <!--web啟動器-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 引入freeMarker的依賴包. -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

        <!--jsp渲染檢視-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

        <!-- jdbcTemplate 依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- mysql 依賴 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!--mybatis依賴-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>

        <!-- 測試 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

</project>

2、配置檔案引入application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false
spring.datasource.username=root
spring.datasource.password=sswqzx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

3、相關類

application.java

package com.springboot;

        import org.mybatis.spring.annotation.MapperScan;
        import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 10:52 2018/11/27
 */
//掃包註解掃mapper包
@MapperScan("com.springboot.mapper")
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

MyController.java

package com.springboot.controller;

import com.springboot.domain.User;
import com.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * springboot整合mybatis
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 19:29 2018/11/28
 */
@Controller
public class MyController {

    @Autowired
    private UserService userService;

    @ResponseBody //將資料以json格式返回給瀏覽器
    @RequestMapping("/finduser")
    public User findByName(){
        User user = userService.findByName("ssw");
        return user;
    }

    @RequestMapping("/inserts")
    public String testInsert(){
        String insert ="";
        Boolean boos = userService.inserts("bbs", "pswd");
       if (boos){
           insert = "insert";
       }
       return insert;
    }
}

UserService.java介面

package com.springboot.service;

import com.springboot.domain.User;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 17:18 2018/11/28
 */
public interface UserService {
    public abstract User findByName(String name);
    public abstract Boolean inserts(String name ,String password);
}

UserServiceImpl.java實現類

package com.springboot.service.Impl;

import com.springboot.domain.User;
import com.springboot.mapper.UserMapper;
import com.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 17:19 2018/11/28
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByName(String name) {
        return userMapper.findByName(name);
    }

    @Override
    public Boolean inserts(String name, String password){
        int num = userMapper.inserts(name, password);
        Boolean flag = false;
        if (num > 0){
            flag = true;
        }
        return flag;
    }
}

UserMapper.java介面

package com.springboot.mapper;

import com.springboot.domain.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 18:38 2018/11/28
 */
public interface UserMapper {

    @Select("select * from user where name = #{name}")
    User findByName(@Param("name")String name);

    @Insert("insert into user(name,password) values(#{name},#{password})")
    int inserts(@Param("name")String name, @Param("password")String password);
}

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false
spring.datasource.username=root
spring.datasource.password=sswqzx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

insert.ftl

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title>首頁</title>
</head>
<body>
<h1>mybatis整合新增資料</h1>
</body>
</html>

 User.java實體類

package com.springboot.domain;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 18:40 2018/11/28
 */
public class User {
    private int id;
    private String name;
    private String password;

    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 String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

4、測試

http://localhsot:8080/inserts

http://localhost:8080/finduser