背景:框架搭建(一)未将mapper语句与方法分离,可将语句配置成xml,项目路径如下
1.在原框架基础上application.yml里添加mapper和实体类配置
mybatis: #mybatis配置 mapper-locations: classpath:mapper/*.xml type-aliases-package: Class4.entity
2.resources下新建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="Class4.mapper.UserinfoMapper"> <resultMap id="ResultMap" type="Class4.entity.UserInfo"> <id column="id" property="id" /> <id column="age" property="age" /> <id column="info" property="info" /> <id column="name" property="name" /> </resultMap> <select id="selectById" resultMap="ResultMap"> select id,age,name,info from test_user_info where id = #{id,jdbcType=TINYINT} </select> <insert id="insert" > insert into test_user_info values(#{id,jdbcType=TINYINT},#{age,jdbcType=INTEGER},#{name,jdbcType=VARCHAR},#{info,jdbcType=VARCHAR}) </insert> <insert id="update" > update test_user_info set name='jiangger' where id= #{id,jdbcType=TINYINT} </insert> </mapper>
3.mapper/UserinfoMapper简化
package Class4.mapper; import Class4.entity.UserInfo; import org.apache.ibatis.annotations.*; import org.apache.ibatis.type.JdbcType; import org.springframework.stereotype.Repository; import java.util.List; /** * @author grjiang * @description:mapper * @date 2021-07-02 18:47 */ @Mapper public interface UserinfoMapper { List<UserInfo> selectById(int id); int insert(int id, int age,String name,String info); int update(int id); }
4.控制层controller/MysqlController, 修改注解