MyBatis 笔记(二)——XML vs 注解
来源:互联网 发布:linux远程控制mingling 编辑:程序博客网 时间:2024/06/08 02:14
MyBatis 支持注解和 XML 两种配置。这一节延续 MyBatis 笔记(一)继续讲。
1. 基于 XML 的配置
基于 XML 的配置其实和 MyBatis 笔记(一)几乎是一样的。先看最基础的配置文件:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/test?useSSL=true"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <!-- 基于XML配置文件 --> <mapper resource="edu/wzm/mybatis/mapping/PersonMapper.xml"/> </mappers></configuration>
最后一行指向 XML 映射文件,XML 映射文件配置要执行的 SQL 语句:
<?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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的 例如:namespace="edu.wzm.mybatis.mapping.PersonMapper"(userMapper.xml文件去除后缀)--><mapper namespace="edu.wzm.mybatis.mapping.PersonMapper"> <!-- 在select、insert、update、delete标签中编写查询的SQL语句, 设置标签的id属性值必须是唯一的,不能够重复。使用parameterType属性 指明查询时,使用的参数类型。resultType属性指明查询返回的结果集类型,resultType="edu.wzm.mybatis.domain.Person"就表示将查询 结果封装成一个Person类的对象返回,Person类就是person表所对应的实体类。 --> <insert id="insert" parameterType="edu.wzm.mybatis.domain.Person"> insert into person(name, age) values(#{name}, #{age}) </insert> <select id="getById" resultType="edu.wzm.mybatis.domain.Person"> select * from person where id = #{id} </select> <select id="getAll" resultType="edu.wzm.mybatis.domain.Person"> select * from person; </select> <update id="update" parameterType="edu.wzm.mybatis.domain.Person"> update person set age = #{age} where id = #{id} </update> <delete id="delete" parameterType="int"> delete from person where id = #{id} </delete></mapper>
在用 MyBatis 调用这些 SQL 语句时,需要写出 XML 的全路径,同时加上每个 SQL 语句的 id 名:
public class PersonServices { public void add(){ Person person = new Person("Alice", 30); SqlSession sqlSession = MyBatisUtils.getSqlSession(true); /** * 映射sql的标识字符串。 * edu.wzm.mybatis.mapping.PersonMapper是PersonMapper.xml中mapper标签的namespace属性的值, * insert是insert标签的id属性值,通过update标签的id属性值就可以找到要执行的SQL。 */ int result = sqlSession.insert("edu.wzm.mybatis.mapping.PersonMapper.insert", person); System.out.println(result); sqlSession.close(); } public void getAll(){ SqlSession sqlSession = MyBatisUtils.getSqlSession(); List<Person> persons = sqlSession.selectList("edu.wzm.mybatis.mapping.PersonMapper.getAll"); System.out.println(persons); sqlSession.close(); } public void getById(){ SqlSession sqlSession = MyBatisUtils.getSqlSession(); Person person = sqlSession.selectOne("edu.wzm.mybatis.mapping.PersonMapper.getById", 1); System.out.println(person); sqlSession.close(); } public void update(){ Person person = new Person(); person.setId(7); person.setAge(27); SqlSession sqlSession = MyBatisUtils.getSqlSession(true); int result = sqlSession.update("edu.wzm.mybatis.mapping.PersonMapper.update", person); System.out.println(result); sqlSession.close(); } public void delete(){ SqlSession sqlSession = MyBatisUtils.getSqlSession(true); int result = sqlSession.update("edu.wzm.mybatis.mapping.PersonMapper.delete", 6); System.out.println(result); sqlSession.close(); }}
2. 基于注解的配置
这里我们只看配置这部分,其他部分和 MyBatis 笔记(一)一样。首先是最基础的配置文件:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/test?useSSL=true"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <!-- 基于注解 --> <mapper class="edu.wzm.mybatis.mapping.PersonMapper"/> </mappers></configuration>
最后一行配置指向注解配置的 Mapping 映射文件。这个映射文件是 Java 的接口,每个方法上面会用注解配置 SQL 语句:
public interface PersonMapper { @Insert("insert into person(name, age) values(#{name}, #{age})") public int add(Person person); @Select("select * " + "from person " + "where id = #{id}") public Person getById(int id); @Select("select * from person") public List<Person> getAll(); @Update("update person set age = #{age} where id = #{id}") public int update(Person person); @Delete("delete from person where id = #{id}") public int deleteById(int id);}
在启动 MyBatis 访问数据库时,也和 XML 不同。它不需要写 XML 映射文件的全路径,直接指定接口名即可:
public class PersonServices { public void add(){ Person person = new Person("Kim", 30); SqlSession sqlSession = MyBatisUtils.getSqlSession(true); PersonMapper mapper = sqlSession.getMapper(PersonMapper.class); int result = mapper.add(person); System.out.println(result); sqlSession.close(); } public void getAll(){ SqlSession sqlSession = MyBatisUtils.getSqlSession(); PersonMapper mapper = sqlSession.getMapper(PersonMapper.class); List<Person> person = mapper.getAll(); System.out.println(person); sqlSession.close(); } public void getById(){ SqlSession sqlSession = MyBatisUtils.getSqlSession(); PersonMapper mapper = sqlSession.getMapper(PersonMapper.class); Person person = mapper.getById(1); System.out.println(person); sqlSession.close(); } public void update(){ SqlSession sqlSession = MyBatisUtils.getSqlSession(true); PersonMapper mapper = sqlSession.getMapper(PersonMapper.class); Person person = new Person(); person.setId(6); person.setAge(50); int result = mapper.update(person); System.out.println(result); sqlSession.close(); } public void delete(){ SqlSession sqlSession = MyBatisUtils.getSqlSession(true); PersonMapper mapper = sqlSession.getMapper(PersonMapper.class); int result = mapper.deleteById(7); System.out.println(result); sqlSession.close(); }}
查看完整代码
阅读全文
0 0
- MyBatis 笔记(二)——XML vs 注解
- mybatis学习笔记(二)-- 使用mybatisUtil工具类体验基于xml和注解实现
- Spring Boot学习笔记----mybatis注解(二)
- 二、Mybatis增、删、改、查基于XML和注解
- mybatis学习笔记——Xml配置文件
- MyBatis笔记(二)
- xml笔记(二)——xml文档的规范
- mybatis应用(二)注解的实现
- MyBatis-Spring(二)使用注解
- MyBatis读书学习笔记(二)——映射器
- MyBatis学习笔记(二)——标签使用
- MyBatis学习笔记(十)注解映射
- Mybatis学习笔记--(三)使用注解
- mybatis笔记-4-注解
- 简化Spring的XML配置(二)——使用注解装配bean
- spring系列(二)——AOP的两种实现方式(xml和注解)
- MyBatis多参数传递之注解方式示例——MyBatis学习笔记之十一
- 【Mybatis】深入浅出Mybatis(二)——Mybatis的框架
- 第一周编程题_分数
- 第一篇:浅析线性递归和尾递归
- Udacity Deep Learning课程作业(三)
- 2017年8月19日,我的技术博客之路开始!
- UVA 10798
- MyBatis 笔记(二)——XML vs 注解
- 自定义view
- Mac快速搭建http,ftp服务器
- 第二篇:排序算法——快速排序
- 堆排序的Java实现、性能分析以及适用场景
- TicWatch+android studio开发之ADB调试
- <8/19>集训日记
- [Tyvj1728] 普通平衡树
- 20170819_去除掉重复元素使其唯一并排序