十一、持久层框架(MyBatis)
一、基于注解方式的CRUD
把xml方式的CRUD修改为注解方式
之前在xml中配置,是在<mapper></mapper>标签下写CRUD
<mapper namespace="com.demo.pojo">
<select id="listProduct" parameterType="Product">
select * from product_table
</select>
<insert id="addProduct" parameterType="Product">
insert into product_table (name) values (#{name})
</insert>
<update id="updateProduct" parameterType="Product">
update product_table set name=#{name} where id=#{id}
</update>
<delete id="deleteProduct" parameterType="Product">
delete from product_table where id=#{id}
</delete>
<mapper>
1、因此要增加Mapper接口方式实现
比如要把之前映射文件Product.xml配置方式变成注解方式的,新建一个ProductMapper接口,并在接口中声明的方法上,加上注解就可以(也就是把SQL语句从xml上移到了注解上来)
package com.demo.mapper; import java.util.List; import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update; import com.demo.pojo.Product; public interface ProductMapper{
@Insert(" inert into product_table (name) values (#{name}) ")
public int add(Product product);
@Delete(" delete from product_table where id=#{id} ")
public void delete(int id);
@Select(" select * from product_table where id=#{id} ")
public Product select(int id);
@Update(" update product_table set name=#{name} where id=#{id} ")
public int update(Product product);
@Select(" select * from product_table ")
public List<Product> selectAll();
}
2、在mybatis-config.xml配置文件中,配置对ProductMapper接口的映射,至于原来的Product.xml映射文件是否保留都是随意
<?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>
<typeAliases>
<package name="com.demo.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/demo?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="admin"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--基于xml配置的映射文件-->
<mapper resource="com/demo/pojo/Product.xml"/>
<!--基于注解方式配置的映射-->
<mapper class="com.demo.mapper.ProductMapper"/>
</mappers>
</configuration>
3、TestMyBatis测试如下:
进行简单的CRUD测试。
package com.demo;
import java.io.IOException;
import java.io.InputSteam;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.apache.ibatis.io.Resouces;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.demo.pojo.Product;
import com.demo.mapper.ProductMapper; public class TestMyBatis{
public static void main(String[] args) throws IOException{
String resouce="mybatis-config.xml";
InputSteam inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session=sqlSessionFactory.openSession();
ProductMapper mapper=session.getMapper(ProductMapper.class);//加载基于注解方式配置的映射文件。 select(mapper);
insert(mapper);
update(mapper);
delete(mapper);
selectAll(mapper); session.commit();
session.close(); } private static void select(ProductMapper mapper){
Product p=mapper.get(1);//获取id=1的产品
System.out.println(p);
} private static void insert(ProductMapper mapper){
Product p=new Product();
p.setName("新增一个product");
mapper.add(p);
selectAll(mapper);
} private static void update(ProductMapper mapper){
Product p=new Product();
p.setId(1);
mapper.update(p);
selectAll(mapper);
} private static void delete(ProductMapper mapper){
mapper.delete(1);
selectAll(mapper);
} private static void selectAll(ProductMapper mapper){
List<Product> list=mapper.list();
for(Product p:list){
System.out.println(p);
}
}
}
十一、持久层框架(MyBatis)的更多相关文章
- 从零搭建springboot服务02-内嵌持久层框架Mybatis
愿历尽千帆,归来仍是少年 内嵌持久层框架Mybatis 1.所需依赖 <!-- Mysql驱动包 --> <dependency> <groupId>mysql&l ...
- java持久层框架mybatis如何防止sql注入
看到一篇很好的文章:http://www.jfox.info/ava-persistence-framework-mybatis-how-to-prevent-sql-injection sql注入大 ...
- Java数据持久层框架 MyBatis之背景知识三
摘录自:http://www.cnblogs.com/lcngu/p/5437281.html 对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.or ...
- Java数据持久层框架 MyBatis之API学习一(简介)
对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...
- Java数据持久层框架 MyBatis之背景知识二
对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...
- Java数据持久层框架 MyBatis之背景知识一
对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...
- 开源顶级持久层框架——mybatis(ibatis)——day02
mybatis第二天 高级映射 查询缓存 和spring整合 课程复习: mybatis是什么? mybatis是一个持久层框架,mybatis ...
- Java持久层框架Mybatis入门
MyBatis是什么 MyBatis是Java的持久层框架,GitHub的star数高达15.8k,是Java技术栈中最热门的ORM框架之一.它支持自定义SQL.存储过程以及高级映射,可以通过XML或 ...
- 开源顶级持久层框架——mybatis(ibatis)——day01
mybatis-day01 1.对原生态jdbc程序中的问题总结 1.1环境 java环境:jdk eclipse:indigo ...
- 整合使用持久层框架mybatis 使用SqlSessionTemplate模板类与使用映射接口 对比
spring中整合使用mybatis的用法总结 一:在Spring配置Mybatis 第一步:将mybatis-spring类包添加 到项目的类库中 第二步:编写spring和持久层衔接的xml文件, ...
随机推荐
- 移动端开发:使用jQuery Mobile还是Zepto
原:http://blog.csdn.net/liubinwyzbt/article/details/51446771 jQuery Mobile和Zepto是移动端的js库.jQuery Mobil ...
- BZOJ 1061: [Noi2008]志愿者招募(线性规划与网络流)
http://www.lydsy.com/JudgeOnline/problem.php?id=1061 题意: 思路: 直接放上大神的建模过程!!!(https://www.byvoid.com/z ...
- python学习 day014打卡 内置函数二&递归函数
本节主要内容: 1.lambda匿名函数 2.sorted() 3.filter() 4.map() 5.递归函数 6.二分法 一.lambda匿名函数 为了解决一些简单的需求而设计的一句话函数 # ...
- Spring-MVC依赖
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api& ...
- ZJOI-2017 R1游记
无实力非既得利益的$xrdog$作为一名外卡选手去参加ZJOI2017啦... Day 0: 颓?(细节待填坑..) Day 1: 上午我来到讲课现场发现讲课内容是:搜索专题 QwQ不太清醒的我一下 ...
- Linux系统vi或者vim编辑器中如何显示行号
设置行号很简单 我们要到vi或者vim编辑器的命令模式下,输入set number :set number 按下回车就显示行号了 那么怎么关闭行号呢? 很简单,我们只要再到vi或者vim编辑器的命令模 ...
- Memcached遇到的问题及解决办法
1. memcached make: *** No targets specified and no makefile found. Stop. 其实是因为在安装libevent时增加了版本号导致的, ...
- linux 下设置定时任务
Linux Crontab 定时任务 命令详解 在工作中需要数据库在每天零点自动备份所以需要建立一个定时任务.我选择在Linux下使用Crontab来添加定时任务执行shell文件.shell文件有数 ...
- 开发自己的R包(转)
R不必说,数据统计分析可视化的必备语言,R包开发的门槛比较低,所以现在随便一篇文章都会发表一个自己的R包,这样有好处(各种需求早有人帮你解决了)也有坏处(R包太多,混乱,新手上手较难).作为生信工程师 ...
- java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration 最近在做项目的时候遇到了这个问题,很是困扰,多次尝试后发现是jar包的问 ...