您好,登錄后才能下訂單哦!
MyBatis視圖與Spring Boot集成案例
一、項目準備
二、配置MyBatis
application.properties
或application.yml
中配置MyBatis的相關屬性,如數據源、映射文件位置等。示例:
# application.properties
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.demo.entity
示例實體類:
package com.example.demo.entity;
public class User {
private Long id;
private String name;
private Integer age;
// 省略getter和setter方法
}
示例映射文件:
<?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.example.demo.mapper.UserMapper">
<resultMap id="UserResultMap" type="com.example.demo.entity.User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</resultMap>
<select id="selectUserById" resultMap="UserResultMap">
SELECT * FROM user WHERE id = #{id}
</select>
</mapper>
三、配置視圖
application.properties
或application.yml
中配置視圖的相關屬性,如視圖名稱、模板引擎等。示例:
# application.properties
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
user.html
。示例視圖文件:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>User Details</title>
</head>
<body>
<h1>User Details</h1>
<p th:text="${user.name}"></p>
<p th:text="${user.age}"></p>
</body>
</html>
四、集成MyBatis視圖與Spring Boot
示例Mapper接口:
package com.example.demo.mapper;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
List<User> selectAll();
}
示例Mapper實現類(使用注解方式):
package com.example.demo.mapper;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("SELECT * FROM user")
List<User> selectAll();
}
或者使用XML方式:
示例映射文件(UserMapper.xml):
<?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.example.demo.mapper.UserMapper">
<resultMap id="UserResultMap" type="com.example.demo.entity.User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</resultMap>
<select id="selectAll" resultMap="UserResultMap">
SELECT * FROM user
</select>
</mapper>
示例Mapper接口:
package com.example.demo.mapper;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
List<User> selectAll();
}
示例Controller:
package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
@Controller
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/users")
public String listUsers(Model model) {
List<User> users = userMapper.selectAll();
model.addAttribute("users", users);
return "user";
}
}
五、運行項目
啟動Spring Boot項目,訪問http://localhost:8080/users
,即可看到MyBatis視圖與Spring Boot集成的效果。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。