本文使用IDEA创建一个Spring Boot的项目,连接MySQL数据库,使用MyBatis实现数据库查询操作。
1建立Spring Boot项目
使用IDEA创建Spring Initializr项目,勾选常用的组件,通过Maven添加到项目中。
项目的POM文件如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.2.1.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.example</groupId> 12 <artifactId>demo</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>demo</name> 15 <description>Demo project for Spring Boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 </properties> 20 21 <dependencies> 22 <dependency> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-starter-amqp</artifactId> 25 </dependency> 26 <dependency> 27 <groupId>org.springframework.boot</groupId> 28 <artifactId>spring-boot-starter-data-elasticsearch</artifactId> 29 </dependency> 30 <dependency> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-starter-data-jdbc</artifactId> 33 </dependency> 34 <dependency> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-starter-data-mongodb</artifactId> 37 </dependency> 38 <dependency> 39 <groupId>org.springframework.boot</groupId> 40 <artifactId>spring-boot-starter-data-redis</artifactId> 41 </dependency> 42 <dependency> 43 <groupId>org.springframework.boot</groupId> 44 <artifactId>spring-boot-starter-thymeleaf</artifactId> 45 </dependency> 46 <dependency> 47 <groupId>org.springframework.boot</groupId> 48 <artifactId>spring-boot-starter-web</artifactId> 49 </dependency> 50 <dependency> 51 <groupId>org.mybatis.spring.boot</groupId> 52 <artifactId>mybatis-spring-boot-starter</artifactId> 53 <version>2.1.1</version> 54 </dependency> 55 56 <dependency> 57 <groupId>mysql</groupId> 58 <artifactId>mysql-connector-java</artifactId> 59 <scope>runtime</scope> 60 </dependency> 61 <dependency> 62 <groupId>org.springframework.boot</groupId> 63 <artifactId>spring-boot-starter-test</artifactId> 64 <scope>test</scope> 65 <exclusions> 66 <exclusion> 67 <groupId>org.junit.vintage</groupId> 68 <artifactId>junit-vintage-engine</artifactId> 69 </exclusion> 70 </exclusions> 71 </dependency> 72 <dependency> 73 <groupId>org.springframework.amqp</groupId> 74 <artifactId>spring-rabbit-test</artifactId> 75 <scope>test</scope> 76 </dependency> 77 <dependency> 78 <groupId>org.webjars</groupId> 79 <artifactId>jquery</artifactId> 80 <version>3.4.1</version> 81 </dependency> 82 </dependencies> 83 84 <build> 85 <plugins> 86 <plugin> 87 <groupId>org.springframework.boot</groupId> 88 <artifactId>spring-boot-maven-plugin</artifactId> 89 </plugin> 90 </plugins> 91 </build> 92 93 </project>
2添加Modle层文件,com.example.demo.model.Users:
1 package com.example.demo.model; 2 3 public class Users { 4 public Integer id; 5 public String last_name; 6 public Integer gender; 7 public String email; 8 9 public Users() { 10 } 11 12 public Users(Integer id, String last_name, Integer gender, String email) { 13 this.id = id; 14 this.last_name = last_name; 15 this.gender = gender; 16 this.email = email; 17 } 18 19 public Integer getId() { 20 return id; 21 } 22 23 public void setId(Integer id) { 24 this.id = id; 25 } 26 27 public String getLast_name() { 28 return last_name; 29 } 30 31 public void setLast_name(String last_name) { 32 this.last_name = last_name; 33 } 34 35 public Integer getgender() { 36 return gender; 37 } 38 39 public void setgender(Integer gender) { 40 this.gender = gender; 41 } 42 43 public String getemail() { 44 return email; 45 } 46 47 public void setemail(String eamil) { 48 this.email = eamil; 49 } 50 51 @Override 52 public String toString() { 53 return "Users{" + 54 "Id=" + id + 55 ", LastName='" + last_name + '\'' + 56 ", Gender=" + gender + 57 ", Eamil='" + email + '\'' + 58 '}'; 59 } 60 }
3数据库中创建表,tbl_employee:
1 SET NAMES utf8; 2 SET FOREIGN_KEY_CHECKS = 0; 3 4 -- ---------------------------- 5 -- Table structure for `tbl_employee` 6 -- ---------------------------- 7 DROP TABLE IF EXISTS `tbl_employee`; 8 CREATE TABLE `tbl_employee` ( 9 `id` int(11) NOT NULL AUTO_INCREMENT, 10 `last_name` varchar(255) DEFAULT NULL, 11 `gender` char(1) DEFAULT NULL, 12 `email` varchar(255) DEFAULT NULL, 13 PRIMARY KEY (`id`) 14 ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1; 15 16 -- ---------------------------- 17 -- Records of `tbl_employee` 18 -- ---------------------------- 19 BEGIN; 20 INSERT INTO `tbl_employee` VALUES ('1', 'yang', '1', 'francs1@sina.com'); 21 COMMIT; 22 23 SET FOREIGN_KEY_CHECKS = 1;
3配置MyBatis
3.1配置文件application.properties
1 spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver 2 spring.datasource.url = jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8 3 spring.datasource.username = root 4 spring.datasource.password = root 5 6 7 spring.data.mongodb.uri=mongodb://localhost:27017/blog
8 mybatis.config-location=classpath:mybatis-config.xml
9 mybatis.mapper-locations=classpath:UserMapper.xml
3.2Mybatis全局配置文件,mybatis-config.xml:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> 3 <configuration> 4 </configuration>
3.3Mybatis映射文件,UserMapper.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 <!-- namespace 所对应的是一个接口的全限定名--> 4 <mapper namespace="com.example.demo.mapper.IUserMapper"> 5 <select id="getUserById" resultType="com.example.demo.model.Users"> 6 SELECT id,last_name,gender,email FROM tbl_employee WHERE id = #{id} 7 </select> 8 </mapper>
3.4Mybatis接口文件,com.example.demo.mapper.IUserMapper:
1 package com.example.demo.mapper; 2 3 import com.example.demo.model.Users; 4 import org.springframework.stereotype.Component; 5 6 @Mapper 7 public interface IUserMapper { 8 public Users getUserById(Integer id); 9 }
4添加Service层文件,调用Mybatis实现数据库操作:
1 @Service 2 public class Users { 3 4 @Autowired 5 IUserMapper userMapper; 6 7 public com.example.demo.model.Users getUserById(Integer id) throws IOException { 8 return userMapper.getUserById(id); 9 } 10 }
5添加Controller文件:
1 package com.example.demo.controller; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.web.bind.annotation.PathVariable; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RestController; 7 8 import java.io.IOException; 9 10 @RestController 11 public class UserController { 12 13 @Autowired 14 com.example.demo.service.Users userService; 15 16 @RequestMapping("/user/{id}") 17 public String getUserById(@PathVariable Integer id) throws IOException { 18 19 com.example.demo.model.Users users = userService.getUserById(id); 20 return users.getLastName(); 21 } 22 }
6测试:
启动文件
1 package com.example.demo; 2 3 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 7 import java.io.IOException; 8 import java.io.InputStream; 9 10 @SpringBootApplication 11 public class DemoApplication { 12 13 public static <StudentMapper> void main(String[] args){ 14 15 SpringApplication.run(DemoApplication.class, args); 16 } 17 18 }
Spring Boot运行在本机8080端口,通过地址:[http://localhost:8080/user/1],完成查询操作。