mybatis学习笔记-实现查询用户信息功能

来源:互联网 发布:javascript与html论文 编辑:程序博客网 时间:2024/05/17 09:00
建立pojo文件
public class User {// 属性名称和user表一致private int id;private String username;private Date birthday;private String sex;private String address;......}

建立映射文件
在映射文件中配置SQL语句。
映射文件名:
User.xml(原始ibatis命名),mapper代理开发映射文件名称叫xxxMapper.xml,比如UserMapper.xml。

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- namespace命名空间,作用就是对sql进行分类化管理,sql隔离 注意:使用mapper代理方法开发,namespace有特殊重要作用 --><mapper namespace="test"><!-- 在映射文件中配置很多sql语句 --><!-- 通过select执行数据库查询 --><!-- 需求:通过id查询用户表记录 --><!-- id:标识映射文件中的sql 将sql语句封装到mapperStatement对象中,所以将id称为statement的id --><!-- parameterType:指定输入参数的类型,这里指定int型 --><!-- #{}表示一个占位符 --><!-- #{id}:其中的id表示接收输入的参数,参数名称就是id,如果输入参数是简单类型,#{}中的参数名可以任意,可以value或其他名称 --><!-- resultType:指定sql输出结果的所映射的Java对象类型,select指定resultType表示将单条记录映射成的Java对象 --><select id="findUserById" parameterType="int" resultType="sys.mybatis.pojo.User">select * from user where id = #{id}</select><!-- ${}表示拼接sql串,将收到参数的内容不加任何修饰拼接在sql中 --><!-- 使用${}拼接sql,容易引起sql注入 --><!-- ${value} 接收输入参数的内容,如果传入类型是简单类型,${}中只能使用value --><select id="findUsersByName" parameterType="java.lang.String"resultType="sys.mybatis.pojo.User">select * from user where username like '%${value}%'</select></mapper>
在SqlMapConfig.xml 中加载配置文件
<!-- 加载映射文件 --><mappers><mapper resource="sqlmap/User.xml" /></mappers>
建立测试类
public class MybatisFirst {@Testpublic void findUserByIdTest() throws IOException {SqlSession sqlSession = null;try {// mybatis配置文件String resource = "SqlMapConfig.xml";// 得到配置文件流InputStream inputStream = Resources.getResourceAsStream(resource);// 创建会话工厂,传入mybatis的配置文件信息SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);// 通过工厂得到SqlSessionsqlSession = sessionFactory.openSession();// 通过SQLSession操作数据库// 第一个参数:映射文件中statement的id,等于namespace.statement的id// 第二个参数:指定和映射文件中所匹配的parameterType类型的参数// sqlSession.selectOne结果是与映射文件中所匹配的resultType类型的对象User user = sqlSession.selectOne("test.findUserById", 1);System.out.println(user);} catch (Exception e) {System.out.println(e);} finally {if (sqlSession != null) {// 释放资源sqlSession.close();}}}@Testpublic void findUsersByNameTest() throws IOException {SqlSession sqlSession = null;try {String resource = "SqlMapConfig.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);sqlSession = sessionFactory.openSession();List<User> users = sqlSession.selectList("test.findUsersByName", "明");System.out.println(users.size());} catch (Exception e) {System.out.println(e);} finally {if (sqlSession != null) {// 释放资源sqlSession.close();}}}}
注意点
#{}和${}

#{}表示一个占位符号,通过#{}可以实现preparedStatement向占位符中设置值,自动进行java类型和jdbc类型转换,#{}可以有效防止sql注入。
#{}可以接收简单类型值或pojo属性值。 如果parameterType传输单个简单类型值,#{}括号中可以是value或其它名称。
#{}接收pojo对象值,通过OGNL读取对象中的属性值,通过属性.属性.属性...的方式获取对象属性值

${}表示拼接sql串,通过${}可以将parameterType 传入的内容拼接在sql中且不进行jdbc类型转换, ${}可以接收简单类型值或pojo属性值,如果parameterType传输单个简单类型值,${}括号中只能是value。
${}接收pojo对象值,通过OGNL读取对象中的属性值,通过属性.属性.属性...的方式获取对象属性值
parameterType和resultType
parameterType:指定输入参数类型,mybatis通过ognl从输入对象中获取参数值拼接在sql中。
resultType:指定输出结果类型,mybatis将sql查询结果的一行记录数据映射为resultType指定类型的对象。
selectOne和selectList
selectOne查询一条记录
selectList可以查询一条或多条记录。

0 0
原创粉丝点击