MyBatis(3)-映射文件
2018-09-29 04:03:41来源:博客园 阅读 ()
本次博文有疑问,请先看MyBatis(1)-简单入门 和 MyBatis(2)-全局配置文件!
如在有疑问,请留言或者咨询博主,博主每天都在!谢谢!
映射文件:
主要是在xxxmapper.xml文件里的配置
映射文件指导这MyBatis如何进行数据库的增删该查,有着很重要的意义。
下面开始进入正题,最后会发一个完整版的代码
因为实现的主要内容是增删改查,所以我们现在接口类中定义好相关的方法,后面不在进行单一的定义!
工程的目录如下:
EmployeeMapper.java
package com.MrChengs.dao; import java.util.Map; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import com.MrChengs.bean.Employee; public interface EmployeeMapper {
//我们直接传入map public Employee getByMap(Map<String,Object> map); //查询满足两个条件的数据 public Employee getByIdAndNames(@Param("id")int id,@Param("name")String name); //查询 public Employee getEmployee(int id); //增加 public void addEmp(Employee employee); //更新 public void updateEmp(Employee employee); //删除 public void deleteById(int id); }
1)insert-映射插入语句
1.1)EmployeeMapper.xml
<insert id="addEmp" >
insert into test(name,gender,email) values(#{name},#{gender},#{email})
</insert>
@Test public void test2() throws IOException{ //1.得到SqlSeesionFactory SqlSessionFactory sessionFactory = getSqlSessionFactory(); //2.得到SqlSession SqlSession sqlSession = sessionFactory.openSession(); try{ EmployeeMapper mapper = (EmployeeMapper) sqlSession.getMapper(EmployeeMapper.class); //手动添加数据: Employee em = new Employee( "zhangsan", "boy", "zhangsan@qq.com"); mapper.addEmp(em); //手动提交数据 sqlSession.commit(); }finally{ sqlSession.close(); }}
1.3)测试成功
2)update映射更新语句
2.1)EmployeeMapper.xml
<update id="updateEmp"> update test set name=#{name},gender=#{gender} where id=#{id} </update>
2.2)测试类中
@Test public void test3() throws IOException{ //1.得到SqlSeesionFactory SqlSessionFactory sessionFactory = getSqlSessionFactory(); //2.得到SqlSession SqlSession sqlSession = sessionFactory.openSession(); try{ EmployeeMapper mapper = (EmployeeMapper) sqlSession.getMapper(EmployeeMapper.class); //手动添加数据: Employee em = new Employee( 6,"Jerry", "gril", null); mapper.updateEmp(em); //手动提交数据 sqlSession.commit(); }finally{ sqlSession.close();}}
2.3)测试成功!
3)delete-映射删除操作
3.1)EmployeeMapper.xml
<delete id="deleteById"> delete from test where id=#{id} </delete>
3.2)测试类中
@Test public void test4() throws IOException{ //1.得到SqlSeesionFactory SqlSessionFactory sessionFactory = getSqlSessionFactory(); //2.得到SqlSession SqlSession sqlSession = sessionFactory.openSession(); try{ EmployeeMapper mapper = (EmployeeMapper) sqlSession.getMapper(EmployeeMapper.class); //手动添加数据: mapper.deleteById(6); //手动提交数据 sqlSession.commit(); }finally{ sqlSession.close();}}
3.3)测试成功!
4)关于主键的值问题
有时候需要用到主键值的问题,所以我们需要去学习这个知识点!
4.1)EmployeeMapper.xml
<!-- useGeneratedKeys="true"使用自增主键获取主键的值 -->
<!-- keyProperty=""接受对应的主键值-->
<insert id="addEmp" useGeneratedKeys="true" keyProperty="id">
insert into test(name,gender,email) values(#{name},#{gender},#{email})
</insert>
4.2)测试类
@Test public void test2() throws IOException{ //1.得到SqlSeesionFactory SqlSessionFactory sessionFactory = getSqlSessionFactory(); //2.得到SqlSession SqlSession sqlSession = sessionFactory.openSession(); try{ EmployeeMapper mapper = (EmployeeMapper) sqlSession.getMapper(EmployeeMapper.class); //手动添加数据: Employee em = new Employee( "zhangsan", "boy", "zhangsan@qq.com"); mapper.addEmp(em); //获取主键值的属性值进行打印 System.out.println(em.getId()); //手动提交数据 sqlSession.commit(); }finally{ sqlSession.close(); } }
4.3)测试成功
5)参数处理
5.1)单个参数:MyBatis不会做任何处理
#{参数名} : 取出参数值
5.1.1)xml配置文件中
<select id="getId" resultType="com.MrChengs.bean.Employee"> select id,name,gender,email from test where id = #{idabc} </select>
5.1.2)测试类
@Test public void test() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession(); try{ Employee employee = session.selectOne("getId",5); System.out.println(employee); }finally{ session.close(); } }
5.1.3)测试是成功的!
5.2)多个参数时
MyBatis会做特殊的处理
多个参数时封装成map
key param1 ,param2......
value 传入的值
5.2.1)接口类中的查询代码设计如上所展示的
public Employee getByIdAndNames(@Param("id")int id,@Param("name")String name);
此时去掉@param:
public Employee getByIdAndNames(int id,String name);
5.2.2) xml文件
<select id="getByIdAndNames" resultType="com.MrChengs.bean.Employee" > select id,name,gender,email from test where id = #{id} and name=#{name} </select>
5.2.3)测试代码
@Test public void test5() throws IOException{ SqlSessionFactory sessionFactory = getSqlSessionFactory(); SqlSession sqlSession = sessionFactory.openSession(); try{ EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class); Employee employee = mapper.getByIdAndNames(5, "MrChengs"); System.out.println(employee); }finally{ sqlSession.close(); } }
5.2.4)此时会报错
5.2.5)处理方案:是在EmployeeMapper.xml文件中修改的
<!-- 同时使用两个参数进行数据库的查询 --> <!-- public Employee getByIdAndNames(int id,String name); --> <select id="getByIdAndNames" resultType="com.MrChengs.bean.Employee" > select id,name,gender,email from test where id = #{param1}
and name=#{param2}
</select>
此时得到正确的答案!
5.3)多个查询字段时推荐使用的命名字段
public Employee getByIdAndNames(@Param("id")int id,@Param("name")String name);
5.3.2)xml文件中
<select id="getByIdAndNames" resultType="com.MrChengs.bean.Employee" > select id,name,gender,email from test where id = #{id} and name=#{name} </select>
public Employee getByMap(Map<String,Object> map);
5.4.2)xml文件中添加
<select id="getByMap" resultType="com.MrChengs.bean.Employee" > select id,name,gender,email from test where id = #{id} and name=#{name} </select>
5.4.3)测试类
@Test public void test6() throws IOException{ SqlSessionFactory sessionFactory = getSqlSessionFactory(); SqlSession sqlSession = sessionFactory.openSession(); try{ EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class); Map<String, Object> map = new HashMap<>(); map.put("id", 5); map.put("name", "MrChengs"); Employee employee = mapper.getByMap(map); System.out.println(employee); }finally{ sqlSession.close(); } }
运行时,可以正确输出结果!
5.5)思考问题注意
未完,待更新....
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:线程
- MyBatis中的$和#,用不好,准备走人! 2020-06-11
- SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 2020-06-11
- Spring Boot 实现配置文件加解密原理 2020-06-08
- Java跨平台原理(字节码文件、虚拟机) 以及Java安全性 2020-06-07
- 【Java-jxl插件】【Excel文件读写报错】jxl.read.biff.BiffE 2020-06-07
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash