标签:pen 字符 param sql sre lis get ram 实体类
<?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.xf.mapper.UsersMapper">
<!-- 根据id查询用户。id:标识该标签。 parameterType:参数类型。可以写 也可以省略 resultType:返回结果的类型。
#{id}:类似于EL表达式。 解析id的值 -->
<select id="getUser" parameterType="int" resultType="com.zhiyou100.xf.bean.Users">
select * from users where id=#{id}
</select>
<insert id="insertUser" parameterType="com.zhiyou100.xf.bean.Users">
insert into users values(default,#{name},#{age})
</insert>
<update id="updateUser" parameterType="com.zhiyou100.xf.bean.Users">
update users set name=#{name},age=#{age} where id=#{id}
</update>
<select id="selAll" resultType="com.zhiyou100.xf.bean.Users">
select * from users
</select>
<delete id="deleteUser" parameterType="int">
delete from users where id=#{id}
</delete>
<select id="selBy" parameterType="map" resultType="com.zhiyou100.xf.bean.Users">
select * from users where id between ${min} and ${max}(此处使用<>来表示范围可以用转义字符或者特定标签)
</select>
</mapper>
当有多个值传入时,可以选择作为list或者实体类。如果多值的话,会作为map传入
普通测试
public class Test { public static void main(String[] args) throws IOException { //配置解析文件xml Reader reader=Resources.getResourceAsReader("conf.xml"); //获取SessionFactory对象 SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader); //获取session对象 SqlSession session=sessionFactory.openSession(); Users users=session.selectOne("com.zhiyou100.xf.mapper.UsersMapper.getUser",1 ); System.out.println(users); } }
接口与映射文件结合使用(指明接口所在路径)
namespace="com.zhiyou100.xf.dao.UsersDao"
public interface UsersDao {
//方法名必须和sql映射文件中方法的id相同 public Users getUser(int id); public List<Users> selAll(); public void insertUser(Users u); public void deleteUser(int id); public void updateUser(Users u); public List<Users> selll(); //多个参数存入map? public List<Users> selBy(@Param("min") int min,@Param("max") int max); }
测试调用
UsersDao ud=session.getMapper(UsersDao.class);
标签:pen 字符 param sql sre lis get ram 实体类
原文地址:https://www.cnblogs.com/accc111/p/11448893.html