SpringBoot quickly create a project and integrate Mybatis

2019-09-15

A, Maven build environment

  1. Import coordinate jar

 1 <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/maven-v4_0_0.xsd">
 2     <modelVersion>4.0.0</modelVersion>
 3     <groupId>com.panda</groupId>
 4     <artifactId>Panda_Maven_SpringBoot</artifactId>
 5     <packaging>war</packaging>
 6     <version>0.0.1-SNAPSHOT</version>
 7     <name>Panda_Maven_SpringBoot Maven Webapp</name>
 8     <url>http://maven.apache.org</url>
 9     <!-- 父起步依赖 -->
10     <parent>
11         <groupId>org.springframework.boot</groupId>
12         <artifactId>spring-boot-starter-parent</artifactId>
13         <version>2.0.1.RELEASE</version>
14     </parent>
15     <dependencies>
16         <dependency>
17         <groupId>junit</groupId>
18         <artifactId>junit</artifactId>
19         <version>3.8.1</version>
20         <scope>test</scope>
21         </dependency>
22         <!-- web起步依赖 -->
23         <dependency>
24             <groupId>org.springframework.boot</groupId>
25             <artifactId>spring-boot-starter-web</artifactId>
26             <version>2.1.8.RELEASE</version>
27         </dependency>
28         <!-- 热部署依赖 -->
29         < Dependency > 
30              < the groupId > org.springframework.boot </ the groupId > 
31 is              < the artifactId > Spring-Boot-DevTools </ the artifactId > 
32          </ dependency > 
33 is          <-! Automatically prompted profile based on attributes of the content dependent Writing - -> 
34 is          < dependency > 
35              < the groupId > org.springframework.boot </ the groupId > 
36              < the artifactId > Spring-Configuration-Processor Boot- </the artifactId > 
37 [          </ dependency > 
38 is          <-! introducing dependent coordinates associated mybatis -> 
39          < dependency > 
40              < the groupId > org.mybatis.spring.boot </ the groupId > 
41 is              < the artifactId > mybatis-Spring-Boot-Starter </ the artifactId > 
42 is              < Version > 2.1.0 </ Version > 
43 is          </ dependency > 
44 is          <-! introduced mysql coordinate dependence ->
45          <dependency>
46             <groupId>mysql</groupId>
47             <artifactId>mysql-connector-java</artifactId>
48         </dependency>
49     </dependencies>
50     <build>
51         <finalName>Panda_Maven_SpringBoot</finalName>
52     </build>
53 </project>

  2. Create an entry class

 1 package com.panda.controller;
 2 
 3 import org.mybatis.spring.annotation.MapperScan;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 
 7 /**
 8  * @author Jpanda
 9  */
10 //springboot入口类
11 @SpringBootApplication
12 //扫描mappers包
13 @MapperScan("com.panda.mappers")
14 public class SpringBootApplicationConfig {
15     // 程序入口
16     public static void main(String[] args) {
17         // run: Run inlet class parameter is the class bytecode object inlet
 18 is          SpringApplication.run (SpringBootApplicationConfig.class);
 . 19      }
 20 is }

    3. Create a bean class

 1 package com.panda.beans;
 2 
 3 /**
 4  * @author Jpanda
 5  */
 6 public class User {
 7     private Integer id;
 8     private String username;
 9     private String password;
10 
11     public Integer getId() {
12         return id;
13     }
14 
15     public void setId(Integer id) {
16         this.id = id;
17     }
18 
19     public String getUsername() {
20         return username;
21     }
22 
23     public void setUsername(String username) {
24         this.username = username;
25     }
26 
27     public String getPassword() {
28         return password;
29     }
30 
31     public void setPassword(String password) {
32         this.password = password;
33     }
34 
35     public User(Integer id, String username, String password) {
36         super();
37         this.id = id;
38         this.username = username;
39         this.password = password;
40     }
41 
42     public User() {
43         super();
44     }
45 
46     @Override
47     public String toString() {
48         return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
49     }
50 
51 }

      4. Create an interface mapper

 1 package com.panda.mappers;
 2 
 3 import java.util.List;
 4 
 5 import org.apache.ibatis.annotations.Mapper;
 6 
 7 import com.panda.beans.User;
 8 
 9 /**
10  * @author Jpanda
11  */
12 @Mapper
13 public interface UserMapper {
14     public List<User> getUsers();
15 }

       5. Create a User of the database table (omitted)

       6.sql map

1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE mapper
3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5 <mapper namespace="com.panda.mappers.UserMapper">
6     <select id="getUsers" resultType="com.panda.beans.User">
7         select * from user
8     </select>
9 </mapper>

  7. Configure a database connection, and relation springboot mybatis

 1 #DB Configuration:
 2 spring.datasource.driverClassName=com.mysql.jdbc.Driver
 3 spring.datasource.url=jdbc:mysql://localhost:3307/user?useUnicode=true&characterEncoding=utf8
 4 spring.datasource.username=root
 5 spring.datasource.password=root
 6 
 7 #spring and mybatis
 8 #pojo alias package
 9 mybatis.type-aliases-package=com.panda.beans
10 #upload mapperconfig files
11 mybatis.mapper-locations=classpath:UserMapperConfig.xml

  8. Create Controller

 1 package com.panda.controller;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.ResponseBody;
 9 
10 import com.panda.beans.User;
11 import com.panda.mappers.UserMapper;
12 
13 /**
14  * @author Jpanda
15  */
16 @Controller
17 public class MybatisController {
18     @Autowired
19     private UserMapper userMapper;
20 
21     @RequestMapping("/mybatis")
22     @ResponseBody
23     public List<User> getUsers() {
24         List<User> users = userMapper.getUsers();
25         System.out.println(users);
26         return users;
27     }
28 }

  9. Access test

 

Guess you like

Origin www.cnblogs.com/jpanda/p/11521376.html