本文主要参考:https://blog.csdn.net/winter_chen001/article/details/77249029
好,下面上货。
1、新建一个springboot项目。
2、修改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>
<groupId>com.lxhj.justice</groupId>
<artifactId>justice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>justice</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-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>1.3.2</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>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.11</version>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、修改app.properties文件
server.port=9090
spring.datasource.url=jdbc:mysql://localhost:3306/justice?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
4、创建文件目录,包括包和文件夹
5、application.properties文件内容
<?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.lxhj.justice.dao.StudentDao">
<sql id="allField">
id, `name`, age, memo
</sql>
<select id="getAll" resultType="com.lxhj.justice.entity.Student">
select
<include refid="allField"/>
from student
</select>
<select id="getById" resultType="com.lxhj.justice.entity.Student">
SELECT
<include refid="allField"/>
from student WHERE id = #{id}
</select>
<select id="getByNameLike" resultType="com.lxhj.justice.entity.Student">
SELECT
<include refid="allField"/>
from student WHERE name LIKE CONCAT('%',#{name},'%')
</select>
<select id="getByIdAndName" resultType="com.lxhj.justice.entity.Student" parameterType="com.lxhj.justice.entity.Student">
SELECT
<include refid="allField"/>
from student WHERE id = #{id} AND name = #{name}
</select>
<select id="getByIdAndNameWithParamMap" resultType="com.lxhj.justice.entity.Student" parameterType="HashMap">
SELECT
<include refid="allField"/>
from student WHERE id = #{id} AND name = #{name}
</select>
<select id="getByCreateTime" resultType="com.lxhj.justice.entity.Student">
SELECT
<include refid="allField"/>
from student where createtime >= #{date}
</select>
<insert id="insertStudent" parameterType="com.lxhj.justice.entity.Student">
insert INTO student(id, `name`, age, memo)
VALUES (#{id},#{name},#{age},#{memo})
</insert>
<delete id="deleteById">
DELETE FROM student where id = #{id}
</delete>
</mapper>
6、JusticeApplication内容,这里要注意把dao的包在这里引入一下,不然找不到接口。
@SpringBootApplication
@MapperScan(basePackages = {"com.lxhj.justice.dao"})
public class JusticeApplication {
public static void main(String[] args) {
SpringApplication.run(JusticeApplication.class, args);
}
}
7、StudentDao.java
public interface StudentDao {
List<Student> getAll();
Student getById(int id);
List<Student> getByNameLike(String name);
Student getByIdAndName(Student student);
Student getByIdAndNameWithParamMap(HashMap<String, Object> params);
List<Student> getByCreateTime(Date date);
int insertStudent(Student student);
int deleteById(Student student);
}
8、Cros.java用于跨域
@Configuration
public class Cros extends WebMvcConfigurerAdapter {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH")
.allowCredentials(true).maxAge(3600);
}
}
9、这样就齐活了。然后运行,测试即可。
需要注意的是
1、@mapperscan要在入口添加。
2、application.properties里面需要指定mapper的xml文件的位置。
3、跨域问题。