一, IDEA 新建 SpringBoot+MySQL+MyBatis+Lombok+Spring Boot DevTools 项目
1, 新建项目的选中项
2, 新建后我们需要配置勾选这个自动编译
3, 按住 shift+alt+ctrl+/, 然后选中 registry
4, 然后选中这个选项
5, 然后在 pom.xml 文件添加以下代码, 开启热部署工具, 不配置不生效
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <configuration>
- <!-- 必须配置 devtools-->
- <fork>true</fork>
- </configuration>
- </plugin>
- </plugins>
- </build>
6, 启用 lombok, 一个实体类简化插件, 简化了实体的 set get, 浏览安装插件
PS: 如果你的 IDEA 连不上网, 下载不了, 你就去下载插件导入, 不过必须下载对应 IDEA 的版本的插件, 如下导入方法, 选择你下载好的路径
离线下载地址如下:
二, 环境创建和准备好了, 开始撸代码
1, 新建好的项目结构
2, 如下代码
- BaseMapper
- package com.boot.nlssm.bootnlssm.common;
- import tk.mybatis.mapper.common.Mapper;
- import tk.mybatis.mapper.common.MySqlMapper;
- /**
- * 项目中所有 Mapper 类的父类
- * @param <T>
- */
- public interface BaseMapper<T> extends Mapper<T>, MySqlMapper<T> {
- }
- UserInfo
- package com.boot.nlssm.bootnlssm.domain;
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
- import javax.persistence.Id;
- import javax.persistence.Table;
- import java.util.Date;
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- @Table(name = "t_user")
- public class UserInfo {
- @Id // 设定为主键
- private Integer userId;
- private String userName;
- private Date createDate;
- }
- UserInfoMapper
- package com.boot.nlssm.bootnlssm.mapper;
- import com.boot.nlssm.bootnlssm.domain.UserInfo;
- import tk.mybatis.mapper.common.BaseMapper;
- public interface UserInfoMapper extends BaseMapper<UserInfo> {
- }
- BootNlssmApplication
- package com.boot.nlssm.bootnlssm;
- import com.boot.nlssm.bootnlssm.domain.UserInfo;
- import com.boot.nlssm.bootnlssm.mapper.UserInfoMapper;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.context.ConfigurableApplicationContext;
- import tk.mybatis.spring.annotation.MapperScan;
- @SpringBootApplication
- @MapperScan("com.boot.nlssm.bootnlssm.mapper")
- public class BootNlssmApplication {
- public static void main(String[] args) {
- ConfigurableApplicationContext context= SpringApplication.run(BootNlssmApplication.class, args);
- UserInfoMapper mapper = context.getBean(UserInfoMapper.class);
- UserInfo book1 = mapper.selectByPrimaryKey(1);
- UserInfo book2 = mapper.selectByPrimaryKey(2);
- System.out.print(book1.toString());
- context.close();
- }
- }
application.YAML
- spring:
- datasource:
- driver-class-name: com.MySQL.cj.jdbc.Driver
- url: jdbc:MySQL://localhost:3306/test?serverTimezone=UTC&useSSL=false
- username: root
- password: root
- mapper:
- identity: MySQL
- not-empty: true
- mappers:
- - tk.mybatis.mapper.common.Mapper
- - tk.mybatis.mapper.common.MySqlMapper
- logging:
- level:
- com.example.bootssm2.mapper: debug
三, 这时你就可以运行 main 函数执行数据操作, 当然你需要在本地安装数据库 MySQL
表结构代码
- CREATE TABLE `t_user` (
- `user_Id` int(11) NOT NULL,
- `user_Name` varchar(255) DEFAULT NULL,
- `create_Date` datetime DEFAULT NULL,
- PRIMARY KEY (`user_Id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
为什么实体不用拆分呢, 因为框架内自动默认映射驼峰拆分就是 user_Id 对应于 userId
来源: http://www.bubuko.com/infodetail-3352067.html