SpringBoot整合SpringMVC+Mybatis+Thymeleaf+HTML

    xiaoxiao2022-07-02  128

    SpringBoot整合SpringMVC+Mybatis

    1、创建springboot项目,配置pom文件,添加依赖。

    <?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> <groupId>com.ybjt</groupId> <artifactId>springBoot-CRUD</artifactId> <version>1.0-SNAPSHOT</version> <!--引入父项目依赖--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> </parent> <dependencies> <!--SpringBoot启动器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--web启动器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--Mybatis的启动器--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <!--数据库连接驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> <!-- druid 数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.9</version> </dependency> <!--配置文件提示--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <version>2.1.3.RELEASE</version> </dependency> </dependencies> <!--打包插件--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

    application.properties

    # 数据库连接参数配置 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/ibtais spring.datasource.username=root spring.datasource.password=root # 数据库连接池配置 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource # 扫描实体类设置别名,减少重复书写 mybatis.type-aliases-package=com.ybjt.bean

    2、准备数据库

    CREATE TABLE `user` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `USERNAME` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `PASSWORD` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, PRIMARY KEY (`ID`) ) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

    3、实体类

    package com.ybjt.bean; /** * @author zx_h * @create 2019-05-22 9:52 */ public class User { private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; } }

    4、mapper接口

    package com.ybjt.mapper; import com.ybjt.bean.User; /** * @author zx_h * @create 2019-05-22 9:54 */ public interface UserMapper { //根据id查询 User selectOneByID(Integer id); }

    5、xxxMapper.xm文件

    <?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.ybjt.mapper.UserMapper"> <select id="selectOneByID" resultType="user"> select * from user where id=#{id} </select> </mapper>

    6、service接口

    package com.ybjt.service; import com.ybjt.bean.User; /** * @author zx_h * @create 2019-05-22 10:09 */ public interface UserService { /** * 根据id查询 * @param id * @return */ User selectOneByID(Integer id); }

    7、service实现类

    package com.ybjt.service.impl; import com.ybjt.bean.User; import com.ybjt.mapper.UserMapper; import com.ybjt.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @author zx_h * @create 2019-05-22 10:11 */ @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User selectOneByID(Integer id) { return this.userMapper.selectOneByID(id); } }

    8、controller

    package com.ybjt.controller; import com.ybjt.bean.User; import com.ybjt.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; /** * @author zx_h * @create 2019-05-22 10:16 */ @Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; /** * restful风格跳转页面 * @param page * @return */ @RequestMapping("/{page}") public String execute(@PathVariable String page){ return page; } @RequestMapping("show") public String selectOneByID(Integer id, Model model){ System.out.println(id); User user = this.userService.selectOneByID(id); System.out.println(user); model.addAttribute("user",user); return "show"; } }

    9、html页面

    select.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>查询</title> </head> <body> <h4>查询</h4> <form th:action="@{/user/show}" method="get"> <input type="text" name="id"/> <input type="submit" value="查询"/> </form> </body> </html>

    show.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>数据展示</title> </head> <body> <table border="1px"> <tr> <th>编号</th> <th>姓名</th> <th>密码</th> </tr> <tr> <td th:text="${user.id}"></td> <td th:text="${user.username}"></td> <td th:text="${user.password}"></td> </tr> </table> </body> </html>

    10、启动类

    package com; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author zx_h * @create 2019-05-22 10:23 * @MapperScan扫描MyBatis的Mapper接口 */ @SpringBootApplication @MapperScan("com.ybjt.mapper") public class App { public static void main(String[] args) { SpringApplication.run(App.class,args); } }

    11、访问路径

    http://localhost:8080/user/select

    最新回复(0)