Springboot整合mybatis

1、代码结构

 

 

 

controller层

package com.mashibing.springboot04.controller;


import com.mashibing.springboot04.mapper.Account;
import com.mashibing.springboot04.service.AccountService;
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;
import java.util.List;

@Controller
public class MainController {

    @Autowired
    AccountService accountService;

    @RequestMapping("/list")
    @ResponseBody
    public Object list(){
        List<Account> account = accountService.findAll();

        return account;
    }
}

Service层

package com.mashibing.springboot04.service;

import com.mashibing.springboot04.mapper.Account;
import com.mashibing.springboot04.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountService {

    @Autowired
    AccountMapper mapper;
    public List<Account> findAll(){
        List<Account> all = mapper.findAll();
        return all;
    }
}

实体类层

package com.mashibing.springboot04.mapper;

public class Account {
    private int id;
    private String loginName;
    private String password;
    private String nickName;
    private int age;
    private String location;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getPassword() {
        return password;
    }

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

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }
}

mapper层

package com.mashibing.springboot04.mapper;


import org.apache.ibatis.annotations.Mapper;

import java.util.List;

//ibatis  mybatis mybatis-plus
@Mapper
public interface AccountMapper {

    List<Account> findAll();

}

mapper.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">


<!--namespace对应到接口上就不需要实例化了-->
<mapper namespace="com.mashibing.springboot04.mapper.AccountMapper">
    <!--pojo对象和表之间的字段和属性的映射关系-->
    <resultMap type="com.mashibing.springboot04.mapper.Account" id="BaseResultMap">
        <!--column表示数据库表里面的字段名称,property表示pojo实体类中的属性-->
        <result column="login_name" property="loginName"/>
        <result column="password" property="password"/>
    </resultMap>

    <!--一个select实现-->
    <select id="findAll" resultMap="BaseResultMap">
        select * from account
    </select>
</mapper>

spring启动类

package com.mashibing.springboot04;

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

@SpringBootApplication
@MapperScan(value = "com.mashibing.springboot04.mapper")
public class Springboot04Application {

    public static void main(String[] args) {
        SpringApplication.run(Springboot04Application.class, args);
    }

}

application.properties

# 项目的启动服务端口号
server.port=80
# 项目的起始路径
server.servlet.context-path=/account

#spring.thymeleaf.prefix= classpath:/templates/
#spring.thymeleaf.mode=LEGACYHTML5  # 不进未关闭标签检查,需配合nekohtml使用
#spring.thymeleaf.cache=false

# 数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456


#用来示例化mapper接口
mybatis.type-aliases-package=com.mashibing.springboot04.mapper
# 表映射到sql上的xml文件在哪  sql语句
mybatis.mapper-locations=classpath://mybatis/mapper/*.xml

pom文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mashibing</groupId>
    <artifactId>springboot04</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot04</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.0.RELEASE</spring-boot.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.0.RELEASE</version>
                <configuration>
                    <mainClass>com.mashibing.springboot04.Springboot04Application</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

        <!-- 如果不添加此节点mapper.xml文件都会被漏掉。 -->
        <resources>

            <resource>

                <directory>src/main/java</directory>

                <includes>

                    <include>*.properties</include>

                    <include>**/*.xml</include>

                </includes>

                <filtering>false</filtering>

            </resource>

        </resources>
    </build>

</project>

 

页面展示:

 https://github.com/piziniao/course201901

https://github.com/pagehelper/Mybatis-PageHelper

posted @ 2020-09-14 19:46  suke_123  阅读(123)  评论(0编辑  收藏  举报