Mybatis入门:2(xml形式的增删改查)
xml形式的增删改查
这里感觉没啥好讲的,照着代码自己敲一遍、认真再看看应该都懂的。
Maven工程坐标
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>Mybatis02</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.20</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.4</version></dependency></dependencies></project>
dao接口
package com.dao;import com.domain.User;import java.util.List;public interface UserDao {/*** 普通查询操作* @return*/List<User>findAll();/*** 增添操作* @param user*/void addUser(User user);/*** 根据产品类型删除* @param type*/void deleteUserByType(String type);/*** 修改用户信息* @param user*/void updateUser(User user);/*** 模糊查询操作* @return*/List<User>findByType(String user);
}
实现类
package com.domain;import java.io.Serializable;
import java.util.Date;public class User implements Serializable {private String product_name;private String product_type;private int product_price;private Date regist_date;public String getProduct_name() {return product_name;}public void setProduct_name(String product_name) {this.product_name = product_name;}public String getProduct_type() {return product_type;}public void setProduct_type(String product_type) {this.product_type = product_type;}public int getProduct_price() {return product_price;}public void setProduct_price(int product_price) {this.product_price = product_price;}public Date getRegist_date() {return regist_date;}public void setRegist_date(Date regist_date) {this.regist_date = regist_date;}@Overridepublic String toString() {return "User{" +"product_name='" + product_name + '\'' +", product_type='" + product_type + '\'' +", product_price=" + product_price +", regist_date=" + regist_date +'}';}
}
Mybatis主配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="mysql"><environment id="mysql"><transactionManager type="JDBC"></transactionManager><dataSource type="POOLED"><property name="driver" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/study"/><property name="username" value="root"/><property name="password" value="123456"/></dataSource></environment>
</environments><mappers><mapper resource="com/dao/IUserDao.xml"></mapper></mappers>
</configuration>
映射配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.dao.UserDao"><!--查询--><select id="findAll" resultType="com.domain.User">select * from product</select><!--增添--><insert id="addUser" parameterType="com.domain.User">insert into product(product_name,product_type,product_price,regist_date) value (#{product_name},#{product_type},#{product_price},#{regist_date})</insert><!--删除--><delete id="deleteUserByType" parameterType="String">delete from product where product_type=#{product_type}</delete><!--修改--><update id="updateUser" parameterType="com.domain.User">update product set product_name=#{product_name},product_price=#{product_price},regist_date=#{regist_date} where product_type=#{product_type}</update><select id="findByType" parameterType="String" resultType="com.domain.User">select * from product where product_type like #{product_type}</select>
</mapper>
测试类
package com.test;import com.dao.UserDao;
import com.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.List;public class MybatisTest {private static UserDao userDao;private static SqlSession session;private static InputStream inputStream;@Beforepublic void test() throws Exception{inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();SqlSessionFactory factory = builder.build(inputStream);session = factory.openSession();userDao = session.getMapper(UserDao.class);}@Testpublic void testFindAll(){ //普通查询操作List<User>users = userDao.findAll();for (User user:users){System.out.println(user);}}@Testpublic void testAddUser(){ //增添操作User user = new User();user.setProduct_name("竖笛");user.setProduct_type("乐器");user.setProduct_price(3000);try{user.setRegist_date(new SimpleDateFormat("yyyy-MM-dd").parse("2020-7-16"));}catch (Exception e){e.printStackTrace();}userDao.addUser(user);session.commit(); //对表格进行修改操作(增删改)时必须得提交事务,否则表格不会保存修改
// 若没有写session.commit();则更新的数据没有进行更新
// 进行add和update必须进行session.commit();}@Testpublic void testDelete(){ //删除操作userDao.deleteUserByType("乐器");session.commit(); //对表格进行修改操作(增删改)时必须得提交事务,否则表格不会保存修改}@Testpublic void testUpdate(){ //修改操作User user = new User();user.setProduct_type("乐器");user.setProduct_name("吉他");user.setProduct_price(10000);try{user.setRegist_date(new SimpleDateFormat("yyyy-MM-dd").parse("2020-7-16"));}catch (Exception e){e.printStackTrace();}userDao.updateUser(user);session.commit(); //对表格进行修改操作(增删改)时必须得提交事务,否则表格不会保存修改}@Testpublic void testFindByType(){ //模糊查询操作List<User>userList = userDao.findByType("%用品%");for (User user:userList){System.out.println(user);}}@Afterpublic void destroy() throws Exception{ //资源关闭操作session.close();inputStream.close();}
运行结果
相关文章:

NSDate见解
#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) { // NSDate *date [NSDate date]; // // NSDateFormatter *formatter [[NSDateFormatter alloc] init];// yyyy 年// MM 月// dd 日// HH 24小时 hh 12小时// mm 分钟// ss 秒钟…

秒杀系统架构设计
秒杀活动的技术挑战 1. 对现有网站业务造成冲击 秒杀活动只是网站营销的一个附加活动,这个活动具有时间短,并发访问量大的特点,如果和网站原有应用部署在一起,必须会对现有业务造成冲击,稍有不慎可能导致整个网站瘫痪。…

SpringBoot 2.x 使用 JWT(JSON Web Token)
一、跨域认证遇到的问题 由于多终端的出现,很多的站点通过 web api restful 的形式对外提供服务,采用了前后端分离模式进行开发,因而在身份验证的方式上可能与传统的基于 cookie 的 Session Id 的做法有所不同,除了面临跨域提交 c…

在Unity中制作4种不同的游戏
流派:电子学习| MP4 |视频:h264,1280720 |音频:AAC,48.0 KHz 语言:英语中英文字幕(根据原英文字幕机译更准确)|大小解压后:8.6 GB 含课程素材 |时长:15h 3m Unity 制作4款无代码手机游戏 Make 4 games in Unity with …

Spring学习笔记:1(初步认识概念)
Spring的三大主要特征 spring主要特征有三个:控制反转(IOC),依赖注入(DI)和面向切面(AOP)。 IoC:Inverse of Control(控制反转) 1.对控制反转的…

shell example01
条件判断 if [[ -e ${1} ]]; thenecho "$(tput setaf 2) found ${1} $(tput sgr0)"cat ${1} elseecho "$(tput setaf 1) not found ${1} $(tput sgr0)"exit 1 fi//简化[[ -e ${1} && -e ${2} ]] && cat ${1} > ${2}//判断取反txt4.txti…

gradle教程 [原创](eclipse/ADT下 非插件 非Android Studio/AS)纯手打 第一篇:安装配置gradle...
一个bug 一个脚印的叫你们用gradle。 1介于网络上的很多资料都是老的 不适用与现在的新版本gradle 尤其是有些gradle方法改名了老的用不了 2介于网上都是粘贴复制并且零碎我很蛋疼啊,走了很多歪路才弄出来,所以我弄一个完全完整的版本 3我不但会写gradle…

java的static关键字
java的static关键字 静态变量和静态方法 static关键字最基本的用法是: 1、被static修饰的变量属于类变量,可以通过类名.变量名直接引用,而不需要new出一个类来 2、被static修饰的方法属于类方法,可以通过类名.方法名直接引用&…

Spring学习笔记:2(IOC装配Bean之xml方式)
xml配置方式装配Bean 本文借鉴于:https://www.cnblogs.com/qdhxhz/p/6511887.html Spring框架Bean实例化的方式提供了三种方式实例化Bean 构造方法实例化(默认无参数,用的最多)静态工厂实例化实例工厂实例化 代码如下: Bean1类(构造方法…

学习RPG Maker MZ开发创建并发布PC和移动端游戏
Complete RPG Maker MZ: Create and Publish for PC and Mobile 完整的RPG制造商MZ:为个人电脑和移动设备创建和发布 MP4 |视频:h264,1280720 |音频:AAC,44.1 KHz,2 Ch 语言:英语中英文字幕(根据原英文字幕机译更准确…

CSS选择器总结
总结几种自己比较容易混淆的: 1. 后代选择器,写法是 E1 E2,如 ul li,选择的是所有后代,包括子后代、孙后代…; 2. 子选择器,写法 E1 > E2,只选择子后代,不包括孙后代元素…

OSChina 周六乱弹 —— 小明和网关超经典的故事~
2019独角兽企业重金招聘Python工程师标准>>> 周六,又到了瞎扯淡的时间了。周末,约会,男男女女,还有那啥那啥,你们懂得~ 男人和女人明显不同,这样才导致了异性相吸吗? 1. …

概念艺术绘画学习教程 Schoolism – Foolproof Concept Painting with Airi Pan
Schoolism——万无一失的概念绘画潘 大小解压后:3.19G 含课程素材文件 1920X1080 .mp4 语言:英语中英文字幕(根据原英文字幕机译更准确) 信息: 万无一失的概念绘画潘 本课程由概念设计师兼插画师潘开发,与大家分享她…

Mybatis复习笔记:1
关于模糊查找 模糊查找其实有两种基本操作(之前学的时候看的不太仔细,漏了…) 第一种 <select id"findByType" parameterType"String" resultType"com.domain.User">select * from product where product_type like #{produ…

文件只能安装一次
1 def get_mac_address(): 2 macuuid.UUID(int uuid.getnode()).hex[-12:] 3 return ":".join([mac[e:e2] for e in range(0,11,2)]) 获取计算机名字 1 import socket 2 socket.gethostname() linux下获取主机外网ip 1 import socket2 import fcntl3 impo…

Linux pipe函数
1. 函数说明 pipe(建立管道): 1) 头文件 #include<unistd.h> 2) 定义函数: int pipe(int filedes[2]); 3) 函数说明: pipe()会建立管道,并将文件描写叙述词由參数filedes数组返回。 fi…

操作系统知识点:全面
操作系统知识点:全面 https://www.jianshu.com/p/c3a3cc0254b1 https://www.jianshu.com/u/881ef7b85f62 posted on 2019-09-03 21:44 竹径风声 阅读(...) 评论(...) 编辑 收藏 转载于:https://www.cnblogs.com/girl1314/p/11455906.html

在UE4中创建CG动画 How to create a movie in Unreal Engine 4 using Metahuman
MP4 |视频:h264,1280720 |音频:AAC,44.1 KHz 语言:英语中英文字幕(根据原英文字幕机译更准确)|大小解压后:1.55 GB |时长:1h 16m 你会学到什么 如何在虚幻引擎4中创建CG动画 虚幻引擎4 Metahuman 使用metahuman在虚幻引…

MyBatis复习笔记2:配置文件详解
配置文件详解 属性(properties) MyBatis可以使用 properties 来引入外部 properties 配置文件的内容 resource:引入类路径下的资源 url:引入网络路径或者磁盘路径下的资源 properties 有三种方式使用: 1、通过 reso…

Android ActionBarDrawerToggle、DrawerLayout、ActionBar 结合
ActionBarDrawerToggle是一个开关。用于打开/关闭DrawerLayout抽屉 ActionBarDrawerToggle 提供了一个方便的方式来配合DrawerLayout和ActionBar。以实现推荐的抽屉功能。 即点击ActionBar的homebutton,就可以弹出DrawerLayout抽屉。 在Activity中的两个回调函数中使…

【转】statfs获得硬盘使用情况 模拟linux命令 df
原文网址:http://blog.csdn.net/mociml/article/details/5335474 说明:本文以主要为转载内容,同时加入了我在使用过程中遇到问题对其的修正!!!!!!!࿰…

SQL常见的面试题
SQL常见的面试题 https://www.jianshu.com/p/558f2113bb62 posted on 2019-09-03 21:55 竹径风声 阅读(...) 评论(...) 编辑 收藏 转载于:https://www.cnblogs.com/girl1314/p/11455959.html

Mybatis入门:4(多表查询操作)
多表查询操作 Mybatis的多表操作 表之间的关系有几种:一对多、一对一、多对一、多对多 举例: 用户和订单就是一对多——一个用户可以下多个订单 订单和用户就是多对一——多个订单属于同一个用户 人和身份证号就是一对一 一个人只能有一个身份证号 一个身份证号只…

Python for虚幻引擎编辑器工具脚本学习教程
Python for Unreal Engine Editor Tools Scripting MP4 |视频:h264,1280720 |音频:AAC,44.1 KHz,2 Ch 语言:英语中英文字幕(根据原英文字幕机译更准确) |时长:23节课(4h 8m) |大小解压后:2.7 GB 含课程文件…

C++ Windows进程管理
功能: 1.各个进程启动、挂起、恢复、停止等 2.监听进程的运行状态,进程退出(正常、非正常)时,通知用户 3.异步队列 4.线程安全 进程管理器类: #ifndef __ProcessManager_ProcessManager_H__ #define __Proc…

shell中和||的使用方法
&&运算符:command1 && command2&&左边的命令(命令1)返回真(即返回0,成功被执行)后,&&右边的命令(命令2)才能够被执行;换句话说,“如果…

SQL中内连接、外连接、交叉连接
SQL中内连接、外连接、交叉连接 SQL连接可以分为内连接、外连接、交叉连接。 数据库数据: book表 stu表 1.内连接 1.1.等值连接:在连接条件中使用等于号()运算符比较被连接列的列值,其查询结果中列…

Blender从头开始装配和动画制作低多边形风格的FPS手臂
Rigging and Animating Low Poly FPS Arms in Blender MP4 |视频:h264,1280720 |音频:AAC,44.1 KHz,2 Ch 语言:英语中英文字幕(根据原英文字幕机译更准确) |时长:21节课(4h 56m) |大小解压后:3.16 GB 含课程…

Mybatis复习笔记3:映射文件详解
映射文件详解 参数处理(#和$的区别) #{}:可以获取map中的值或者实体对象属性的值;${}:可以获取map中的值或者实体对象属性的值; select * from person where id${id} and name#{name} # 控制台输出&…
TMS320F28335项目开发记录2_CCS与JTAG仿真器连接问题汇总
CCS与仿真器连接问题 实际使用过程中。仿真器和CCS连接可能出现这样或那样的问题,或许你的连接非常成功,没碰到过什么问题。但我的问题的确不少,可能与电脑配置有关吧,也可能与人品有关吧。 以下的自己的一些错误和解决方法总…