2019-09-15
一、Maven环境搭建
1.导入jar坐标
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.panda</groupId>
<artifactId>Panda_Maven_SpringBoot</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Panda_Maven_SpringBoot Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- 父起步依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- web起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.8.RELEASE</version>
</dependency>
<!-- 热部署依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- 自动根据属性内容提示配置文件的书写依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<!-- 导入关联mybatis坐标依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!-- 导入mysql坐标依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<finalName>Panda_Maven_SpringBoot</finalName>
</build>
</project>
2.创建入口类
package com.panda.controller; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; /**
* @author Jpanda
*/
//springboot入口类
@SpringBootApplication
//扫描mappers包
@MapperScan("com.panda.mappers")
public class SpringBootApplicationConfig {
// 程序入口
public static void main(String[] args) {
// run:运行入口类,参数即为入口类的字节码对象
SpringApplication.run(SpringBootApplicationConfig.class);
}
}
3.创建bean类
package com.panda.beans; /**
* @author Jpanda
*/
public class User {
private Integer id;
private String username;
private String password; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public User(Integer id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
} public User() {
super();
} @Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
} }
4.创建mapper接口
package com.panda.mappers; import java.util.List; import org.apache.ibatis.annotations.Mapper; import com.panda.beans.User; /**
* @author Jpanda
*/
@Mapper
public interface UserMapper {
public List<User> getUsers();
}
5.创建User对应数据库表(省略)
6.sql映射
<?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.panda.mappers.UserMapper">
<select id="getUsers" resultType="com.panda.beans.User">
select * from user
</select>
</mapper>
7.配置数据库连接,关系springboot和mybatis
#DB Configuration:
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3307/user?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root #spring and mybatis
#pojo alias package
mybatis.type-aliases-package=com.panda.beans
#upload mapperconfig files
mybatis.mapper-locations=classpath:UserMapperConfig.xml
8.创建Controller
package com.panda.controller; import java.util.List; 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 com.panda.beans.User;
import com.panda.mappers.UserMapper; /**
* @author Jpanda
*/
@Controller
public class MybatisController {
@Autowired
private UserMapper userMapper; @RequestMapping("/mybatis")
@ResponseBody
public List<User> getUsers() {
List<User> users = userMapper.getUsers();
System.out.println(users);
return users;
}
}
9.访问测试