SpringBoot集成MyBatis

SpringBoot集成MyBatis

1.先创建一个Springboot的web项目
2.创建controller、mapper、service、model四层文件夹
项目路径如下:
在这里插入图片描述
User实体类

public class User {
    private String id;
    private String name;
    private String createuser;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCreateuser() {
        return createuser;
    }

    public void setCreateuser(String createuser) {
        this.createuser = createuser;
    }

    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", createuser='" + createuser + '\'' +
                '}';
    }
}

userMapper接口:

@Repository
public interface userMapper {

    @Select("SELECT * FROM user where id=#{id}")
    User selectId(@Param("id") int id);

    @Select("select * from user")
    List<User> selectAll();

    @Delete("delete from user where id=#{id}")
    void deleteUser(@Param("id") int id);

    @Update("update user set name=#{name} , password=#{password} where id=#{id}")
    void updateUser(@Param("name") String name ,@Param("password") String password,@Param("id") int id);
}

UserService:

@Service
public class UserService {
    @Autowired
    private userMapper usermapper;

    public List<User> selectAll(){
        return usermapper.selectAll();
    }

    public void deleteUser(int id){
        usermapper.deleteUser(id);
    }

    public User selectId(int id){
       return usermapper.selectId(id);
    }

    public void updateUser(String name,String password,int id){
        usermapper.updateUser(name,password,id);
    }

}

controller:

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/selectAll")
    public String list(Model model){
        List<User> ulist = userService.selectAll();
        model.addAttribute("users",ulist);
        return "user/main";
    }

    @DeleteMapping("/deleteUser/{id}")   //@PathVariable("id")   从请求路径上获取id
    public String deleteUser(@PathVariable("id") int id){
        userService.deleteUser(id);
        return "redirect:/selectAll";
    }

     @PostMapping("/updateUser/updateu")
    public String update(@RequestParam("id") int id,
                         @RequestParam("name") String name,
                         @RequestParam("password") String password){
        System.out.println(id+name+password);
        userService.updateUser(name,password,id);
        return "redirect:/selectAll";
    }

}

application.properties:

server.port=8090
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
#serverTimezone时区(GMT%2B8:北京时间东八区),如果设置为UTC(UTC代表的是全球标准时间)则数据库操作的显示时间会比当前时间早8小时
#或者使用上海时间    serverTimezone=Asia/Shanghai            在url这里不设置时区,则可能报错
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/user?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.typeAliasesPackage=com.pzh.entry

#配置新的请求方式(put、delete等)
spring.mvc.hiddenmethod.filter.enabled=true

pom.xml

<!--引入thymeleaf-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--引入maybatis、mysql-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.2.0</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<!--引入json、jquery-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.41</version>
</dependency>
<dependency>
    <groupId>org.webjars.bower</groupId>
    <artifactId>jquery</artifactId>
    <version>3.3.1</version>
</dependency>

main.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>主页面</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
        <div th:id="userdiv">
            <table th:align="center" style="width:300px; min-height:50px; line-height:25px; text-align:center; border:1px solid #ccc; border-collapse: collapse;">
                <thead>
                    <tr>
                        <th>id</th>
                        <th>name</th>
                        <th>password</th>
                        <th>修改</th>
                        <th>删除</th>
                    </tr>
                </thead>
                <tbody style="color:red">
                    <tr th:each="user:${users}" th:border="5">
                        <td th:text="${user.id}"></td>             <!-- 两种方式都可以取出值-->
                        <td>[[${user.name}]]</td>
                        <td>[[${user.password}]]</td>
                        <td><a class="btn btn-sm btn-success" th:href="@{updateUser/}+${user.id}">修改</a></td>
<!--                        <td><a class="btn btn-sm btn-warning" th:href="@{deleteUser/}+${user.id}">删除</a></td>-->
                        <td><button th:attr="del_uri=@{deleteUser/}+${user.id}" class="btn btn-sm btn-warning deleteBtn">删除</button></td>
                    </tr>
                </tbody>
            </table>
            <form id="deleteUsers" method="post">
                <input type="hidden" name="_method" value="delete"/>
            </form>
        </div>
</body>
<script>
    $(".deleteBtn").click(function () {
        $("#deleteUsers").attr("action",$(this).attr("del_uri")).submit();
        return false;
    });
</script>
</html>

update.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>修改</title>
</head>
<body>
<div>
    <form th:action="@{updateu}" method="post" th:align="center">
        <input name="id" th:type="text" th:value="${uid.id}"></input>             <!-- 两种方式都可以取出值-->
        <br><br>
        <input name="name" th:type="text" th:value="${uid.name}"></input>
        <br><br>
        <input name="password" th:type="password" th:value="${uid.password}"></input>
        <br><br>
        <input th:type="submit" value="修改">
    </form>

</div>

</body>
</html>

最后在启动器类上加上@MapperScan(“com.pzh.dao”) //扫描mapper下的类文件
启动项目,输入127.0.0.1:8090/selectAll
在这里插入图片描述数据库表user
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值