一,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的版本的插件,如下导入方法,选择你下载好的路径
离线下载地址如下:https://github.com/mplushnikov/lombok-intellij-plugin/releases
二,环境创建和准备好了,开始撸代码
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.yml
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