创建mybatisdb数据库,设置utf8字符集。
创建user表:
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`user_id` int(11) NOT NULL AUTO_INCREMENT COMMENT ‘用户id‘,
`user_name` varchar(100) COLLATE utf8mb4_bin NOT NULL DEFAULT ‘‘ COMMENT ‘用户名‘,
`birthday` bigint(20) NOT NULL DEFAULT ‘0‘ COMMENT ‘生日‘,
`address` varchar(200) COLLATE utf8mb4_bin NOT NULL DEFAULT ‘‘ COMMENT ‘住址‘,
`create_time` bigint(20) NOT NULL DEFAULT ‘0‘ COMMENT ‘创建时间‘,
`update_time` bigint(20) NOT NULL DEFAULT ‘0‘ COMMENT ‘更新时间‘,
`create_by` varchar(64) COLLATE utf8mb4_bin NOT NULL DEFAULT ‘‘ COMMENT ‘创建者ID或名称‘,
`update_by` varchar(64) COLLATE utf8mb4_bin NOT NULL DEFAULT ‘‘ COMMENT ‘更新者ID或名称‘,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=56 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT=‘商品品牌‘;
insert into `user`(`user_name`,`birthday`,`address`,`create_time`,`create_by`) values
(‘张三‘,1546393586074,‘桂州村‘, 1546393586074, ‘admin‘),
(‘李四‘,1546393586074,‘王府井‘, 1546393586074, ‘admin‘);
1.1.3 创建SpringBoot工程,创建simple project,类型为jar
额外需要的jar,还得自己依赖,例如:mysql驱动包,阿里的数据源druid包
pasting
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Spring Boot整合SSM</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<mybatis-plus.version>2.1.9</mybatis-plus.version>
<druid.version>1.1.5</druid.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--数据库连接为mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--durid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.example.demo.entity;
import lombok.Data;
import java.io.Serializable;
@Data
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
// 不用Date类型做日期,因为实际开发中数据库表不会用Date类型
// @DateTimeFormat(pattern="yyyy-MM-dd")
// private Date birthday;
private String birthday;
private String address;
}
<?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.staritgp.platform.demo.mapper.SysDeptMapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.example.demo.entity.User">
<id column="user_id" property="userId" />
<result column="user_name" property="userName" />
<result column="birthday" property="birthday" />
<result column="address" property="address" />
<result column="create_time" property="createTime" />
<result column="update_time" property="updateTime" />
<result column="create_by" property="createBy" />
<result column="update_by" property="updateBy" />
</resultMap>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
user_id AS userId, user_name AS userName, birthday AS birthday, address AS address, create_time AS createTime, update_time AS updateTime, create_by AS createBy, update_by AS updateBy
</sql>
<select id="findAll" resultType="com.example.demo.entity.User">
select
<include refid="Base_Column_List"></include>
from sys_dept where 1=1
<if test="userName != null and userName != ‘‘">
<!-- 名称模糊查询 -->
AND user_name like CONCAT(‘%‘,#{userName},‘%‘)
</if>
<if test="userId != null and userId != ‘‘">
AND user_id = #{userId}
</if>
<if test="createTime != null">
AND create_time = #{createTime}
</if>
<if test="updateTime != null">
AND update_time = #{updateTime}
</if>
<if test="createBy != null and createBy != ‘‘">
AND create_by = #{createBy}
</if>
<if test="updateBy != null and updateBy != ‘‘">
AND update_by = #{updateBy}
</if>
</select>
</mapper>
package com.example.demo.mapper;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper {
/**
* 自定义分页sql,调用xml方式
* @param user
* @return
*/
List<User> findAll(User user);
// 调用注解方式
@Select("select * from user where user_id=#{id}")
public User findById(Integer id);
}
package com.example.demo.service.impl;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override public User findById(Integer id) {
return userMapper.findById(id);
}
@Override public List<User> findAll(User user) {
return userMapper.findAll(user);
}
}
package com.example.demo.service.impl;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override public User findById(Integer id) {
return userMapper.findById(id);
}
@Override public List<User> findAll(User user) {
return userMapper.findAll(user);
}
}
使用@RestController替代@Controller和@ResponseBody(返回json串)
package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/find")
public List<User> findAll(User user) {
return userService.findAll(user);
}
@RequestMapping("/get/{id}")
public User findById(@PathVariable Integer id){
return userService.findById(id);
}
}
全局配置文件,yml为新的配置文件方式,注意其中格式为空格,不能有tab。
配置端口,配置数据源,配置mybatis全局配置。
注意:如果端口,启动时日志显示8080,说明此文件未加载。检查原因一般是文件名或者路径不正确。
# 服务端口
server:
port: 8081
# 数据连接池配置,druid 比 c3p0 还牛逼
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mybatisdb
username: root
password: root
# com.example.demo 的子目录中的内容都会打印日志
logging:
level:
com.example.demo: debug
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
// 扫描该包下的所有内容,什么mapperscan都不用管,约定大于配置
@ComponentScan(basePackages = {"com.example.demo"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
各组件都有注解形式的自动配置类,可以点进去看看,Spring帮我们定义了一个约定,遵守这个约定可以少好多事情,而且还不容易出错。
spring-boot-autoconfigure-1.5.4.RELEASE.jar
mybatis-spring-boot-autoconfigure-1.3.0.jar
pagehelper-spring-boot-autoconfigure-1.2.2.jar
剩下的CRUD方法,你们可以自行完善一下当做练手。
package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/find")
public List<User> findAll(User user) {
return userService.findAll(user);
}
@RequestMapping("/get/{id}")
public User findById(@PathVariable Integer id){
return userService.findById(id);
}
@RequestMapping("/update")
public String update(User user) {
return userService.update(user);
}
// delete不做物理删除,只是标记为删除
@RequestMapping("/delete")
public Boolean finddeleteAll(User user) {
return userService.delete(user);
}
@RequestMapping("/add")
public User add(User user) {
return userService.add(user);
}
}
原文:https://www.cnblogs.com/wood-life/p/10302041.html