- 创建一个新的maven项目
填写好项目名称,下一步
填写好存储位置,完成
- 加上mybatis的依赖,修改pom.xml 文件
<!--继承springboot的父级依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.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>
<!--springboot开发web程序的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--加载mybatis整合springboot(起步依赖)-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--mysql的jdbc驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
- 在springboot 的核心配置文件application.properties 中,在resource下面手动创建,配置数据库连接的配置信息
mybatis.mapper-locations=classpath:com/nanianxiatian/springboot/mapper/*.xml
##数据库连接配置信息
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1 :3306/datatest?useUnicode=true&characterEncoding=utf8&useSSL=false
添加 包文件 : com.nanianxiatian.springboot.mapper
- 用反向工程生成实体类 在项目中添加xml 文件 :GeneratorMapper.xml ,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 指定连接数据库的JDBC驱动包所在位置,指定到你本机的完整路径,需确保本地路径下存在该jar -->
<classPathEntry location="E:\Maven\repository\mysql\mysql-connector-java\5.1.18/mysql-connector-java-5.1.18.jar"/>
<!-- 配置table表信息内容体,targetRuntime指定采用MyBatis3的版本 -->
<context id="tables" targetRuntime="MyBatis3">
<!--序列化-->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<!-- 抑制生成注释,由于生成的注释都是英文的,可以不让它生成 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 配置数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/datatest"
userId="root"
password="root">
</jdbcConnection>
<!-- 生成model类,targetPackage指定model类的包名, targetProject指定生成的model放在eclipse的哪个工程下面-->
<javaModelGenerator targetPackage="com.nanianxiatian.springboot.model" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="false" />
</javaModelGenerator>
<!-- 生成MyBatis的Mapper.xml文件,targetPackage指定mapper.xml文件的包名, targetProject指定生成的mapper.xml放在eclipse的哪个工程下面 -->
<sqlMapGenerator targetPackage="com.nanianxiatian.springboot.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- 生成MyBatis的Mapper接口类文件,targetPackage指定Mapper接口类的包名, targetProject指定生成的Mapper接口放在eclipse的哪个工程下面 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.nanianxiatian.springboot.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 数据库表名及对应的Java模型类名 -->
<table tableName="student"
domainObjectName="Student"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
- 在pom文件中添加mybatis 代码自动生成的插件
<build>
<plugins>
<!--mybatis 代码自动生成的插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<configurationFile>GeneratorMapper.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
双击自动生成代码的插件,出现 BUILD SUCCESS 即为成功!
- 添加Application.class 以及 main 方法
package com.nanianxiatian.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author nanianxitian
* @date 2020-8-24 20:08
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
在Mybatis的Mapper接口中添加 @Mapper 注解 或者
在运行主类上添加 @MapperScan(“com.nanianxiatian.springboot.mapper”)注解包扫描
我们的代码:
MybatisController.java
package com.nanianxiatian.springboot.controller;
import com.nanianxiatian.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author nanianxiatian
* @date 2020-8-24 20:12
*/
@RestController
public class MybatisController {
@Autowired
private StudentService studentService;
@GetMapping("/boot/students")
public Object students(){
return studentService.getAllStudent();
}
}
StudentService.java
package com.nanianxiatian.springboot.service;
import com.nanianxiatian.springboot.model.Student;
import java.util.List;
/**
* @author nanianxiatian
* @date 2020-8-24 20:15
*/
public interface StudentService {
public List<Student> getAllStudent();
}
StudentServiceImpl.java
package com.nanianxiatian.springboot.service.impl;
import com.nanianxiatian.springboot.mapper.StudentMapper;
import com.nanianxiatian.springboot.model.Student;
import com.nanianxiatian.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author nanianxiatian
* @date 2020-8-24 20:16
*/
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public List<Student> getAllStudent() {
return studentMapper.selectAllStudent();
}
}
StudentMapper.java
package com.nanianxiatian.springboot.mapper;
import com.nanianxiatian.springboot.model.Student;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
public interface StudentMapper {
int deleteByPrimaryKey(Integer id);
int insert(Student record);
int insertSelective(Student record);
Student selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(Student record);
int updateByPrimaryKey(Student record);
List<Student> selectAllStudent();
}
StudentMapper.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.nanianxiatian.springboot.mapper.StudentMapper">
<resultMap id="BaseResultMap" type="com.nanianxiatian.springboot.model.Student">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="age" jdbcType="INTEGER" property="age" />
</resultMap>
<sql id="Base_Column_List">
id, name, age
</sql>
<select id="selectAllStudent" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from student
</select>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from student
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from student
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.nanianxiatian.springboot.model.Student">
insert into student (id, name, age
)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}
)
</insert>
<insert id="insertSelective" parameterType="com.nanianxiatian.springboot.model.Student">
insert into student
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
name,
</if>
<if test="age != null">
age,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="age != null">
#{age,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.nanianxiatian.springboot.model.Student">
update student
<set>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
<if test="age != null">
age = #{age,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.nanianxiatian.springboot.model.Student">
update student
set name = #{name,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
启动之后发现如下错误:
出现这个错误主要是因为编译后的xml没有编译出来,可以看到target下面没有xml文件,这个是idea才会出现的情况
我们需要在pom.xml加上:
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/webaoo</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
刷新之后,还是报错:
原因是因为MySQL版本过高,MySQL升级到8.0及以上,添加了许多新特性,安全性也得到提升。当然操作时也增加了些繁琐,需要考虑到的时区问题便是其中之一。
在url路径的问号后面加上: &serverTimezone=Asia/Shanghai
spring.datasource.url=jdbc:mysql://127.0.0.1 :3306/datatest?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
然后出现正确结果: