您好,登錄后才能下訂單哦!
MyBatis視圖與Spring Boot集成案例
一、項(xiàng)目準(zhǔn)備
二、配置MyBatis
application.properties
或application.yml
中配置MyBatis的相關(guān)屬性,如數(shù)據(jù)源、映射文件位置等。示例:
# application.properties
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.demo.entity
示例實(shí)體類:
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
中配置視圖的相關(guān)屬性,如視圖名稱、模板引擎等。示例:
# 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實(shí)現(xiàn)類(使用注解方式):
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";
}
}
五、運(yùn)行項(xiàng)目
啟動(dòng)Spring Boot項(xiàng)目,訪問http://localhost:8080/users
,即可看到MyBatis視圖與Spring Boot集成的效果。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。