标签:string oid after name XML 类型 insert into val
1.定义sql映射文件
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"> <!-- namespace:表示名称空间。现在的目的是区分id的. --> <mapper namespace="com.zhiyou100.wyf.dao.UseraDao"> <!-- 根据id查询用户。id:标识该标签。 parameterType:参数类型。可以写 也可以省略 resultType:返回结果的类型。 #{id}:类似于EL表达式。 解析id的值 --> <select id="getUsers" parameterType="int" resultType="com.zhiyou100.wyf.bean.Users"> select * from users where id = #{id} </select> <insert id="add" parameterType="com.zhiyou100.wyf.bean.Users"> insert into users(name,age) values(#{name},#{age}) </insert> <delete id="delete" parameterType="com.zhiyou100.wyf.bean.Users"> delete from users where id =#{id} </delete> <update id="update" parameterType="com.zhiyou100.wyf.bean.Users"> update users set name=#{name},age=#{age} where id=#{id} </update> </mapper>
抽象接口userDao中的代码:
package com.zhiyou100.wyf.dao; import java.util.List; import org.apache.ibatis.annotations.Param; import com.zhiyou100.wyf.bean.Users; public interface UseraDao { /** * 根据id查询用户 * @param id * @return */ public Users getUsers(int id); /** * 根据id删除用户 * @param id */ public void delete(int id); /** * 增加用户 * @param id */ public void add(Users user); /** * 修改用户 * @param id */ public void update(Users user); /** * 查询用户 * @param id */ public List<Users> selectbyid(@Param("min") int min,@Param("max") int max); }
单元测试类代码如下:
package com.test; import static org.junit.jupiter.api.Assertions.*; import java.io.Reader; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import com.zhiyou100.wyf.bean.Users; import com.zhiyou100.wyf.dao.UseraDao; class TestMy { static SqlSession session = null; static String str ="com.zhiyou100.wyf.mapper.UsersMapper"; @BeforeAll static void setUpBeforeClass() throws Exception { //解析配置文件conf.xml Reader reader = Resources.getResourceAsReader("conf.xml"); //获取SessionFactory对象 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); session=sessionFactory.openSession(); } @Test void testck() { UseraDao useraDao = session.getMapper(UseraDao.class); Users user = useraDao.getUsers(1); System.out.println(user); } @Test void testdelete() { UseraDao useraDao = session.getMapper(UseraDao.class); useraDao.delete(3); } @Test void testadd() { UseraDao useraDao = session.getMapper(UseraDao.class); useraDao.add(new Users("张麻子",196)); } @Test void testupdate() { UseraDao useraDao = session.getMapper(UseraDao.class); useraDao.update(new Users(5,"张麻子",17)); } @AfterAll static void tearDownAfterClass() throws Exception { session.commit(); } }
2.解决字段与属性不匹配
2.1解决方案为给查询的字段起别名,要求别名与属性名一致。
2.2使用resultMap标签来定义实体类与字段之间的对应关系
https://www.cnblogs.com/xdp-gacl/p/4262895.html
mybatis(二)MyBatis的crud操作以及字段与属性不匹配的问题
标签:string oid after name XML 类型 insert into val
原文地址:https://www.cnblogs.com/yufengwang/p/11439865.html