01—mybatis开山篇
![](https://images2015.cnblogs.com/blog/709254/201702/709254-20170216161414363-1961565854.png)
CREATE TABLE `tb_user` (
`Id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`Username` VARCHAR(50) COLLATE utf8_bin DEFAULT NULL,
`Password` VARCHAR(50) COLLATE utf8_bin DEFAULT NULL,
`Nickname` VARCHAR(50) COLLATE utf8_bin DEFAULT NULL,
`Type` INT(11) DEFAULT NULL,
PRIMARY KEY (`Id`)
)
![](https://images2015.cnblogs.com/blog/709254/201702/709254-20170216160049129-459038809.png)
package org.mybatis.test.model; public class User {
private int id;
private String username;
private String password;
private String nickname;
private int type;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
<?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="org.mybatis.test.model.User">
<insert id="add" parameterType="org.mybatis.test.model.User">
insert into tb_user (username,password,nickname,type)
value(#{username},#{password},#{nickname},#{type})
</insert>
<update id="update" parameterType="org.mybatis.test.model.User">
update tb_user set password=#{password},nickname=#{nickname},type=#{type} where id=#{id}
</update> <delete id="delete" parameterType="int">
delete from tb_user where id=#{id}
</delete> <select id="load" parameterType="int" resultType="org.mybatis.test.model.User">
select * from tb_user where id=#{id}
</select> <select id="list" resultType="org.mybatis.test.model.User">
select * from tb_user
</select>
</mapper>
username=root
password=123456
url=jdbc:mysql://localhost:3306/mybatistest
driver=com.mysql.jdbc.Driver
<?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>
<properties resource="jdbc.properties"/>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<!-- 将mapper文件加入到配置文件中 -->
<mappers>
<mapper resource="org/mybatis/test/model/User.xml"/>
</mappers>
</configuration>
package org.mybatis.test.test; import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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.Test;
import org.mybatis.test.model.User; public class TestMyBatis {
@Test
public void testInsertUser() {
try {
//1、创建配置文件(mybatis-config.xml)的输入流
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
//2、创建SQLSessionFactory
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
//3、创建SQLSessioin
SqlSession session = factory.openSession();
//4、调用mapper文件插入数据(调用之前需要将mapper文件加入到mybatis-config.xml中)
User u = new User();
u.setNickname("小助理001");
u.setPassword("123456");
u.setUsername("zhuli001");
u.setType(0);
session.insert("org.mybatis.test.model.User.add", u);
session.commit();
session.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testUpdateUser() {
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession session = factory.openSession();
User u = new User();
u.setNickname("小猪手001");
u.setPassword("111111");
u.setType(0);
u.setUsername("zhushou");
u.setId(1);
session.update("org.mybatis.test.model.User.update",u);
session.commit();
session.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testDeleteUser() {
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory f = new SqlSessionFactoryBuilder().build(is);
SqlSession session = f.openSession();
session.delete("org.mybatis.test.model.User.delete",2);
session.commit();
session.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testLoad() {
SqlSession session = null;
try{
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory f = new SqlSessionFactoryBuilder().build(is);
session = f.openSession();
User u = (User)session.selectOne(User.class.getName()+".load", 1);
System.out.println(u.getNickname());
}
catch (IOException e) {
e.printStackTrace();
}
finally {
session.close();
}
} @Test
public void testList() {
SqlSession session = null;
try{
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory f = new SqlSessionFactoryBuilder().build(is);
session = f.openSession();
List<User> us = session.selectList(User.class.getName()+".list", null);
for (User u : us) {
System.out.println(u.getId()+"-"+u.getUsername()+"-"+u.getNickname()); //逐个输出数组元素的值
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
session.close();
}
} }
01—mybatis开山篇的更多相关文章
- 01—EF开山篇,ORM介绍
我是2014年接触的EF,用了一年多,感觉非常的方便,现在的公司没有使用,最近有朋友接了两个项目找我帮忙,都想使用EF,自己也有断时间没有使用,借着这个机会复习下.Entity Framework,简 ...
- [高并发]Java高并发编程系列开山篇--线程实现
Java是最早开始有并发的语言之一,再过去传统多任务的模式下,人们发现很难解决一些更为复杂的问题,这个时候我们就有了并发. 引用 多线程比多任务更加有挑战.多线程是在同一个程序内部并行执行,因此会对相 ...
- Struts2开山篇【引入Struts、自定义MyStruts框架】
前言 这是Strtus的开山篇,主要是引入struts框架-为什么要引入struts,引入struts的好处是什么-. 为什么要引入struts? 首先,在讲解struts之前,我们来看看我们以前写的 ...
- 【DevOps】团队敏捷开发系列--开山篇
随着软件发布迭代的频率越来越高,传统的「瀑布型」(开发-测试-发布)模式已经不能满足快速交付的需求.2009 年左右 DevOps 应运而生,开发运维一体化,通过自动化工具与流程让整个软件开发构建.测 ...
- WCF开山篇__图片传输
WCF开山篇__图片传输 一. 简介 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序接口,可以翻译为Windows通讯接口,它是. ...
- MyBatis高级篇之整合ehcache缓存框架
MyBatis高级篇之整合ehcache缓存框架 2017-09-01 0 Comments 1,671 Views 0 Times 一.前言 MyBatis为我们提供了Cache接口,也提供 ...
- 01: tornado基础篇
目录:Tornado其他篇 01: tornado基础篇 02: tornado进阶篇 03: 自定义异步非阻塞tornado框架 04: 打开tornado源码剖析处理过程 目录: 1.1 Torn ...
- Mybatis 实用篇(四)返回值类型
Mybatis 实用篇(四)返回值类型 一.返回 List.Map List<User> getUsers(); <select id="getUsers" re ...
- Mybatis 实用篇(三)参数处理
Mybatis 实用篇(三)参数处理 sql 语句中的参数 parameterType 可以省略不写. 一.参数封装 1.1 单个参数处理 public interface UserMapper { ...
随机推荐
- 最新 搜狐java校招面经 (含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.搜狐等10家互联网公司的校招Offer,因为某些自身原因最终选择了搜狐.6.7月主要是做系统复习.项目复盘.LeetCode ...
- sort(()=>{return Math.random()-0.5)}乱序数组不准确
为什么sort(()=>{return Math.random()-0.5)}乱序数组不准确.(注意结合插入排序原理来理解) @1.chrome浏览器对于数组长度10以内为插入排序.反之则快速排 ...
- Vue ---- Vuex 的第一次接触
在 Vue.js 的项目中,如果项目结构简单, 父子组件之间的数据传递可以使用 props 或者 $emit 等方式 http://www.cnblogs.com/wisewrong/p/62660 ...
- SQL SERVER ISDATE函数
定义: ISDATE函数判断指定字符串是否是有效日期. 语法: ISDATE(date) 参数: ①date是需要判定是否是有效日期的字符串 返回值: int型数据 例: 声明:本文是本人查阅网上及 ...
- orcale数据库授权码
Product Code:4t46t6vydkvsxekkvf3fjnpzy5wbuhphqzserial Number:601769password:xs374ca
- scrapy-redis数据去重与分布式框架
数据去重 生成指纹:利用hashlib的sha1,对request的请求体.请求url.请求方法进行加密,返回一个40位长度的16进制的字符串,称为指纹 fp = hashlib.sha1() fp. ...
- Block Breaker HDU - 6699(深搜,水,写下涨涨记性)
Problem Description Given a rectangle frame of size n×m. Initially, the frame is strewn with n×m squ ...
- mysql innodb数据库损坏导致无法启动
生产环境中的mysql突然启动不了,查了原因是innodb库错误,以前就遇到过这个问题,稀里糊涂的没解决,结果导致大量数据丢失.这些又遇到这个问题,果断把那个有问题的数据库移动了别的地方,启动了mys ...
- MySQL 子查询(一)
源自MySQL 5.7 官方手册 13.2.10 Subquery Syntax 〇.MySQL子查询介绍 子查询指的是嵌套在某个语句中的SELECT语句. MySQL支持标准SQL所要求的所有子查询 ...
- winfrom_权限设置_TreeView的相关问题
1.获取TreeView的值: 循环TreeView,获取checked每个节点的Text,串起来用逗号“,”隔开,保存到数据库. List<string> list = new List ...