一、创建项目
通过IDEA的Initializr创建项目,详情参见:Spring Boot入门
二、添加项目依赖
添加Mybatis、Mysql、mybatis-generator(用于生成项目基础代码)依赖
<?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.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.learn</groupId>
<artifactId>mall</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mall</name>
<description>mall project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--添加mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--添加MySQL依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--添加mybatis逆向工程插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
</project>
三、编写generatorConfig.xml配置文件
编写generatorConfig.xml配置文件,将文件放在resources目录下,配置文件官网地址:http://mybatis.org/generator/configreference/xmlconfig.html
<?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>
<!-- 指定数据连接驱动jar地址 -->
<classPathEntry location="D:\software\apache-maven-3.5.2\repository4\mysql\mysql-connector-java\8.0.18\mysql-connector-java-8.0.18.jar"/>
<!-- 一个数据库一个context -->
<context id="MySQLTables" targetRuntime="MyBatis3">
<!-- 注释 -->
<commentGenerator>
<!-- 是否取消注释 -->
<property name="suppressAllComments" value="true"/>
<!-- 是否生成注释代时间戳-->
<property name="suppressDate" value="true"/>
</commentGenerator>
<!-- jdbc连接 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://192.168.211.128:3316/mall?serverTimeZone=UTC&
useUnicode=true&characterEncoding=utf-8&useSSL=true"
userId="root"
password="123456"/>
<!-- 类型转换 -->
<javaTypeResolver>
<!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成实体类地址 -->
<javaModelGenerator targetPackage="com.learn.mall.model.pojo" targetProject="src/main/java">
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
<property name="enableSubPackages" value="true"/>
<!-- 是否针对string类型的字段在set的时候进行trim调用 -->
<property name="trimStrings" value="true"/>
<!--建立的model对象是否不可改变,即生成的Model对象不会有setter方法,只有构造方法-->
<property name="immutable" value="false"/>
</javaModelGenerator>
<!-- 生成mapper映射文件存放位置即xml文件 -->
<sqlMapGenerator targetPackage="mappers" targetProject="src/main/resources">
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成dao类存放位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.learn.mall.model.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 配置表信息 -->
<!--tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample
是否生成 example类 -->
<table tableName="mall_cart"
domainObjectName="Cart" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false" selectByExampleQueryId="false">
</table>
<table tableName="mall_category"
domainObjectName="Category" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false" selectByExampleQueryId="false">
</table>
<table tableName="mall_order"
domainObjectName="Order" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false" selectByExampleQueryId="false">
</table>
<table tableName="mall_order_item"
domainObjectName="OrderItem" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false" selectByExampleQueryId="false">
</table>
<table tableName="mall_product"
domainObjectName="Product" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false" selectByExampleQueryId="false">
</table>
<table tableName="mall_user"
domainObjectName="User" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false" selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
四、启动mybatis-generator生成基础代码
打开IDEA的Maven Projects窗口,点击mybatis-generator启动插件,运行完成后即可生成基础代码
注意:代码生成之后需要给所有的Mapper文件加上@Repository注解,否则Service注入时会报错
package com.learn.mall.model.dao;
import com.learn.mall.model.pojo.Cart;
import org.springframework.stereotype.Repository;
@Repository
public interface CartMapper {
int deleteByPrimaryKey(Integer id);
int insert(Cart record);
int insertSelective(Cart record);
Cart selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(Cart record);
int updateByPrimaryKey(Cart record);
}
五、编写测试demo
1、在application.properties配置文件中配置数据库信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://192.168.211.128:3316\
/mall?serverTimeZone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
mybatis.mapper-locations=classpath:mappers/*.xml
2、编写Service层接口
package com.learn.mall.service;
import com.learn.mall.model.pojo.User;
public interface UserService {
User getUser();
}
3、编写ServiceImpl
package com.learn.mall.service.impl;
import com.learn.mall.model.dao.UserMapper;
import com.learn.mall.model.pojo.User;
import com.learn.mall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public User getUser() {
return userMapper.selectByPrimaryKey(1);
}
}
4、编写Controller
package com.learn.mall.controller;
import com.learn.mall.model.pojo.User;
import com.learn.mall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class UserController {
@Autowired
UserService userService;
@GetMapping("/test")
@ResponseBody
public User getUser(){
return userService.getUser();
}
}
5、浏览器请求测试