十一、持久层框架(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文件, ...
随机推荐
- Spring-json依赖
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jacks ...
- DataTableHelper
public class DataTableHelper { /// <summary> /// 给DataTable增加一个自增列 /// 如果DataTable 存在 identity ...
- Javascript 垃圾回收机制
转载于https://www.cnblogs.com/zhwl/p/4664604.html 一.垃圾回收的必要性 由于字符串.对象和数组没有固定大小,所有当他们的大小已知时,才能对他们进行动态的存储 ...
- Panda3D
Panda3D 是个开源的游戏及物理引擎(也支持ODE及Bullet). 相关链接:网站: https://www.panda3d.org/下载: https://www.panda3d.org/do ...
- MySQL 并发测试中,线程数和数据库连接池的实验
我一直以来,对性能测试中,连接池的大小要如何配置,不是太清楚: 就我所知道的,就DB自带对连接数的限制,在sqlserver中用select @@connection 可以查到, 在代码中,可以配置D ...
- Xshell5中常用linux服务器命令集合
简易版:http://www.zhimengzhe.com/linux/84546.html 详细版:http://www.cnblogs.com/peida/tag/%E6%AF%8F%E6%97% ...
- leecode第一百二十四题(二叉树中的最大路径和)
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- Python Appium 滑动、点击等操作
Python Appium 滑动.点击等操作 1.手机滑动-swipe # FileName : Tmall_App.py # Author : Adil # DateTime : 2018/3/25 ...
- 解码字符串 Decode String
2018-11-14 17:56:12 问题描述: 问题求解: 方法一.递归求解 最直观的解法就是递归来求了,并且很显然的这个问题可以使用递归来进行求解. public String decodeSt ...
- English trip EM2-LP-6A Teacher:Julia
课上内容(Lesson) How many children are in the family? there are 16 kids How old is the oldest child? He' ...