Springboot integrates mybatisplus + thymeleaf template

Programmer light 2022-02-13 07:52:51 阅读数:410

springboot integrates mybatisplus thymeleaf template

Add dependency

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.15</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>

Add the configuration

server.port=9999
spring.thymeleaf.encoding=utf-8
spring.thymeleaf.mode=html
spring.thymeleaf.prefix=classpath:/templates
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false
spring.thymeleaf.servlet.content-type=text/html
spring.datasource.url=jdbc:mysql://localhost:3306/thymeleaf?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
mybatis-plus.mapper-locations=classpath*:/mapper/*.xml
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

Create database 、 surface

CREATE TABLE `t_user` (
`id` bigint(12) NOT NULL AUTO_INCREMENT COMMENT ' Primary key ID',
`addTime` datetime DEFAULT NULL COMMENT ' New time ',
`userName` varchar(255) DEFAULT NULL COMMENT ' user name ',
`password` varchar(255) DEFAULT NULL COMMENT ' password ',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT=' User table ';

Create entity class

@Data
@TableName("t_user")
public class User {
@TableId(type = IdType.AUTO)
private Long id;
@TableField("addTime")
private Date addTime;
@TableField("userName")
private String userName;
@TableField("password")
private String password;
}

establish Mapper

@Mapper
public interface UserMapper extends BaseMapper<User> {
}

Create business layer interface Service

public interface UserService extends IService<User> {
}

Create a business layer interface implementation class ServiceImpl

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{
}

establish Controller Control layer

@Controller
public class UserController {
@Autowired
UserService userService;
@RequestMapping("/user/login")
public ModelAndView login(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/user/login");
return modelAndView;
}
}

copyright:author[Programmer light],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130752501983.html