后端开发4——mybatis操作数据库模板

1 controller——xxxController.java

点击展开
package com.wrj.controller;
import …
//当前类注册为控制器类并添加到Bean列表 //当需要返回视图页面时需要使用Controller注解,需要返回序列化数据时使用RestController注解 @RestController //映射HTTP请求,RequestMapping不限制请求类型,限制时可使用PostMapping、GetMappering等 @RequestMapping("/personUser") public class PersonUserController { //从注册的Bean列表中自动匹配属性注入对象 @Autowired private IPersonUserService personUserService; //请求处理接口 @RequestMapping("/hello") public String hello(){ try { PersonUser personUser = personUserService.selectPersonUserById("07f1951631d44fb2a03256841fb14c0d"); if(personUser != null) return "loginid: "+personUser.getLoginId().toString(); } catch (Exception e){ return e.getMessage(); } return "hello world"; } }

2 domain——xxx.java

映射数据库表的实体类,与数据表对应;

点击展开
<?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.wrj.mapper.PersonUserMapper">
    <resultMap type="com.wrj.domain.PersonUser" id="PersonUserResult">
        <result property="id"    column="id"    />
        ……
    </resultMap>
<sql id="selectPersonUserVo"> select id, …… from person_user </sql>
<select id="selectPersonUserList" parameterType="com.wrj.domain.PersonUser" resultMap="PersonUserResult"> <include refid="selectPersonUserVo"/> <where> <if test="loginId != null "> and login_id = #{loginId}</if> …… </where> </select>
<select id="selectPersonUserById" parameterType="String" resultMap="PersonUserResult"> <include refid="selectPersonUserVo"/> where id = #{id} </select>
<insert id="insertPersonUser" parameterType="com.wrj.domain.PersonUser"> insert into person_user <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null and id != ''">id,</if> …… </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null and id != ''">#{id},</if> …… </trim> </insert>
<update id="updatePersonUser" parameterType="com.wrj.domain.PersonUser"> update person_user <trim prefix="SET" suffixOverrides=","> <if test="loginId != null">login_id = #{loginId},</if> …… </trim> where id = #{id} </update>
<delete id="deletePersonUserById" parameterType="String"> delete from person_user where id = #{id} </delete> </mapper>

5 resources——mapper——moduleName——xxxMapper.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.wrj.mapper.PersonUserMapper">
    <resultMap type="com.wrj.domain.PersonUser" id="PersonUserResult">
        <result property="id"    column="id"    />
        ……
    </resultMap>
<sql id="selectPersonUserVo"> select id, …… from person_user </sql>
<select id="selectPersonUserList" parameterType="com.wrj.domain.PersonUser" resultMap="PersonUserResult"> <include refid="selectPersonUserVo"/> <where> <if test="loginId != null "> and login_id = #{loginId}</if> …… </where> </select>
<select id="selectPersonUserById" parameterType="String" resultMap="PersonUserResult"> <include refid="selectPersonUserVo"/> where id = #{id} </select>
<insert id="insertPersonUser" parameterType="com.wrj.domain.PersonUser"> insert into person_user <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null and id != ''">id,</if> …… </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null and id != ''">#{id},</if> …… </trim> </insert>
<update id="updatePersonUser" parameterType="com.wrj.domain.PersonUser"> update person_user <trim prefix="SET" suffixOverrides=","> <if test="loginId != null">login_id = #{loginId},</if> …… </trim> where id = #{id} </update>
<delete id="deletePersonUserById" parameterType="String"> delete from person_user where id = #{id} </delete> </mapper>
posted @ 2024-06-15 00:08  wrj的博客  阅读(4)  评论(0)    收藏  举报