spring-boot&mybatis的demo

步骤如下:

1.创建项目:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
点击finish,这样就成功创建了一个maven工程。

2.增加文件目录结构如下:
在这里插入图片描述
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 https://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.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>mybatisdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mybatisdemo</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-data-jdbc</artifactId>
        </dependency>
        <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>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.0</version>
        </dependency>
    </dependencies>

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

</project>

BootController如下:

package com.example.mybatisdemo.controller;

import com.example.mybatisdemo.dao.UserDao;
import com.example.mybatisdemo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * PaclageName com.example.mybatisdemo.controller
 *
 * @Created on  2020/5/19 0019
 * @Author xumingming
 * @DDesrciption
 */

@RestController
@EnableAutoConfiguration
@RequestMapping(value = "/boot")
public class BootController {

    @Autowired
    private UserDao userDao;
    @RequestMapping(value = "getUser")
    public User getUser()
    {
        User user=new User();
        user.setUserName("test");
        return user;
    }

    @RequestMapping(value = "getAllUser")
    public List<User> getUsers()
    {
        List<User> userList= userDao.selectUser();
        return userList;
    }
    }

UserDao:

package com.example.mybatisdemo.dao;

import com.example.mybatisdemo.entity.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * PaclageName com.example.mybatisdemo.entity
 *
 * @Created on  2020/5/19 0019
 * @Author xumingming
 * @DDesrciption
 */
@Mapper
public interface UserDao {
    List<User> selectUser();


}

User:

package com.example.mybatisdemo.entity;

/**
 * PaclageName com.example.mybatisdemo.entity
 *
 * @Created on  2020/5/19 0019
 * @Author xumingming
 * @DDesrciption
 */
public class User {
    private String UserName;

    private Integer id;

    private String address;

    public Integer getId() {
        return id;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getUserName() {
        return UserName;
    }

    public void setUserName(String userName) {
        UserName = userName;
    }
}

SpringBootApplication:

package com.example.mybatisdemo;

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

@MapperScan("com.example.mybatisdemo.dao")
@SpringBootApplication
public class MybatisdemoApplication {

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

}

application.yml:

#开发配置
spring:
  #数据源配置
  datasource:
    url: jdbc:mysql://localhost:3306/codetest?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: dsptest
    password: dsptest
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

#公共配置与profiles选择无关
mybatis:
  typeAliasesPackage: com.example.mybatisdemo.entity
  mapperLocations: classpath:mapper/*.xml

user.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.example.mybatisdemo.dao.UserDao" >
    <resultMap id="BaseResultMap" type="com.example.mybatisdemo.entity.User" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="name" property="userName" jdbcType="VARCHAR" />
        <result column="address" property="address" jdbcType="VARCHAR" />
    </resultMap>
    <sql id="Base_Column_List" >
        id, name, address
    </sql>
    <select id="selectUser" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from user
    </select>
</mapper>

user表:
– auto-generated definition

create table user
(
    address varchar(100) not null,
    name    varchar(20)  not null,
    id      int          not null
);

user数据:
在这里插入图片描述
访问路径:http://localhost:8080/boot/getAllUser
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值