用 Interllij IDEA 創(chuàng)建項(xiàng)目就不貼了
- 添加相關(guān)依賴
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 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.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</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>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory><!--所在的目錄-->
<includes><!--包括目錄下的.properties,.xml文件都會(huì)掃描到-->
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--包含所在的目錄的資源-->
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 編寫 mapper
LoginMapper
@Mapper
public interface LoginMapper {
@Select("SELECT * FROM user WHERE id = #{id}")
User selectUser(int id);
User selectUserOnly(Long id);
}
LoginMapper.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.demo.mapper.LoginMapper">
<select id="selectUserOnly" parameterType="Long" resultType="com.example.demo.bean.User">
SELECT * FROM user WHERE id = #{id}
</select>
</mapper>
- 編寫 Controller
@RestController
public class UserControl {
@Autowired
LoginMapper loginMapper;
@RequestMapping(value = "/user", method = RequestMethod.GET)
public User addNewArticle(@RequestParam(value = "user_id", defaultValue = "1")Integer id) {
User user = loginMapper.selectUser(id);
return user;
}
@RequestMapping(value = "/user_only", method = RequestMethod.GET)
public User selectUserOnly(@RequestParam(value = "user_id", defaultValue = "1")Long id) {
User user = loginMapper.selectUserOnly(id);
return user;
}
}
- 配置文件
application.properties
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#spring.datasource.url=jdbc:mysql://120.79.214.74/vueblog2?useUnicode=true&characterEncoding=UTF-8
spring.datasource.url=jdbc:mysql:///vueblog2?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
mybatis.config-location=classpath:/mybatis-config.xml
#jdbc:mysql://localhost:3306/heima
server.port=8088
logging.level.org.springframework.security=info
- 文件目錄