代码部分:
1: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 http://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.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-mybatis</name>
<packaging>war</packaging>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.0</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--spring-boot 整合 mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
2:entity 包 里 的 是 实体类:随便写几个属性
package com.entity; import java.io.Serializable; public class User implements Serializable { private String name;
private String age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} @Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}
3:mapper文件:
package com.mapper; import com.entity.User;
import java.util.List; import org.springframework.stereotype.Repository; @Repository
public interface UserDao { public List<User> findAll(); public List<User> findOrd(String age);
}
4:service
package com.service; import com.entity.User;
import com.mapper.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class UserService {
/**
* 根据接口查询所用的用户
*/
@Autowired
UserDao userDao; public List<User> findAllUser(){
List<User> all = userDao.findAll();
return all;
}; /*查寻所有的age大于12 的人的信息*/
public List<User> findOrd(String age){
List<User> userDaoOrd = userDao.findOrd(age);
return userDaoOrd;
}
}
5:controller
package com.controller; import com.entity.User;
import com.service.UserService;
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; @RestController
@RequestMapping("/user")
public class Test_controller {
@Autowired
private UserService userService; @RequestMapping("/getuser")
public List<User> getUser() {
List<User> list = userService.findAllUser();
System.out.println(list);
return list;
} @RequestMapping("/order")
public List<User> getOrder(){
String age = "12";
List<User> list = userService.findOrd(age);
return list;
}
}
6:启动类(启动类的位置要在所有的包的上一级,且不能直接放在Java包的下面,需要建一个包放)
package com.controller; import com.entity.User;
import com.service.UserService;
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; @RestController
@RequestMapping("/user")
public class Test_controller {
@Autowired
private UserService userService; @RequestMapping("/getuser")
public List<User> getUser() {
List<User> list = userService.findAllUser();
System.out.println(list);
return list;
} @RequestMapping("/order")
public List<User> getOrder(){
String age = "12";
List<User> list = userService.findOrd(age);
return list;
}
}
7:resouse文件夹下的mapper文件的内容
<?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.mapper.UserDao"> <resultMap id="BaseResultMap" type="com.entity.User">
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="age" jdbcType="VARCHAR" property="age" />
</resultMap> <select id="findAll" resultType="com.entity.User">
select * from user
</select> <!--查询年龄大于 12 的所有user的信息 parameterType:传递参数的类型 resultType:返回值类型 -->
<select id="findOrd" parameterType="String" resultType="com.entity.User">
select * from user where age > #{age}
</select> </mapper>
8:application.yml
#默认使用配置
spring:
profiles:
active: dev
9:application-dev.yml (库是:test 表是:user)
server:
port: 8080 spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/test?userUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.jdbc.Driver mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.entity #showSql
logging:
level:
com:
example:
mapper : debug