Mybaits入门使用
1.pom.xml配置信息
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
</dependencies>
测试类:
public class Person {
private int id;
private String name;
private int age;
private String address;
private String birthday; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + ", birthday="
+ birthday + "]";
}
}
sql语句xml配置:
<?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="test1">
<select id="querypersonbyid" parameterType="int" resultType="person">
select id,name,age,birthday,address from Person where id = #{value}
</select> <select id="querypersonbyname" parameterType="java.lang.String" resultType="person">
select id,name,age,birthday,address from Person where name like "%${value}%"
</select> <insert id="inserperson" parameterType="person">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
insert into Person(name,age,address,birthday) value(#{name},#{age},#{address},#{birthday})
</insert> <delete id="deletepersonbyid" parameterType="int">
delete from Person where id = #{id}
</delete> <delete id="updatepersonbyid" parameterType="person">
update Person set name=#{name},age=#{age},birthday=#{birthday},address=#{address} where id = #{id}
</delete> </mapper>
数据库访问配置:
<?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="db.properties">
<!--properties中还可以配置一些属性名和属性值 -->
<!-- <property name="jdbc.driver" value=""/> -->
</properties> <typeAliases>
<!-- 别名定义 -->
<typeAlias type="com.youfan.entity.Person" alias="person" />
</typeAliases> <environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理,事务控制由mybatis -->
<transactionManager type="JDBC" />
<!-- 数据库连接池,由mybatis管理 -->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments> <!-- 加载 映射文件 -->
<mappers>
<!--通过resource方法一次加载一个映射文件 -->
<!--注意这里的路径和xml文件 -->
<mapper resource="Person.xml" /> </mappers> </configuration>
TestMybatis:
import java.io.IOException;
import java.io.InputStream;
import java.util.List; import com.sun.org.apache.xpath.internal.SourceTree;
import com.youfan.entity.Person;
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 java.io.InputStream; public class TestMybatis {
public SqlSessionFactory getfactory() throws IOException {
String filepath="SqlMappingConfig.xml";
InputStream in = Resources.getResourceAsStream(filepath);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
return sqlSessionFactory;
}
@Test
public void testinsert() throws IOException {
SqlSessionFactory sqlSessionFactory = this.getfactory();
SqlSession sqlsession = sqlSessionFactory.openSession();
Person person = new Person();
person.setName("小高");
person.setAddress("上海");
person.setAge(15);
person.setBirthday("05-04");
sqlsession.insert("test1.inserperson", person);
System.out.println("id "+person.getId());
sqlsession.commit();
sqlsession.close();
} @Test
public void testquerybyid() throws IOException{
SqlSessionFactory sqlSessionFactory = this.getfactory();
SqlSession sqlsession = sqlSessionFactory.openSession();
Person person = sqlsession.selectOne("querypersonbyid", 6);
System.out.println(person);
sqlsession.close();
}
@Test
public void testquerybyname() throws IOException{
SqlSessionFactory sqlSessionFactory = this.getfactory();
SqlSession sqlsession = sqlSessionFactory.openSession();
List<Person> personlist = sqlsession.selectList("querypersonbyname", "小高");
for(int i=0;i<personlist.size();i++){
System.out.println(personlist.get(i));
}
sqlsession.close();
} @Test
public void testdeletebyid() throws IOException{
SqlSessionFactory sqlSessionFactory = this.getfactory();
SqlSession sqlsession = sqlSessionFactory.openSession();
sqlsession.delete("deletepersonbyid", 6);
sqlsession.commit();
sqlsession.close();
} @Test
public void testupdatePerson() throws IOException{
SqlSessionFactory sqlSessionFactory = this.getfactory();
SqlSession sqlsession = sqlSessionFactory.openSession();
Person person = new Person();
person.setId(2);
person.setName("有范");
person.setAddress("北京");
person.setAge(15);
person.setBirthday("06-04");
sqlsession.update("updatepersonbyid", person);
sqlsession.commit();
sqlsession.close();
}
}
表字段:
用映射方法写:
mapper:
public interface PersonMapper {
public Person querypersonbyid(int id);
public List<Person> querypersonbyname(String name);
public void inserperson(Person person);
public void deletepersonbyid(int id);
public void updatepersonbyid(Person person);
public List<Person> querypersonbyvo(PersonVo personVo);
}
视图对象vo:
public class CustomPerson extends Person{ }
public class PersonVo {
private CustomPerson customPerson; public CustomPerson getCustomPerson() {
return customPerson;
} public void setCustomPerson(CustomPerson customPerson) {
this.customPerson = customPerson;
} }
语句:
<?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="com.youfan.mapper.PersonMapper">
<sql id="consutomsql" >
<if test="customPerson != null">
<if test="customPerson.name!=null and customPerson.name!=''">
and name = "${customPerson.name}"
</if>
<if test="customPerson.birthday!=null and customPerson.birthday!='' ">
and birthday = "${customPerson.birthday}"
</if>
</if>
</sql>
<select id="querypersonbyvo" parameterType="personVo" resultType="person">
select * from Person
<where>
<include refid="consutomsql"></include>
</where>
</select>
<select id="querypersonbyid" parameterType="int" resultType="person">
select id,name,age,birthday,address from Person where id = #{value}
</select> <select id="querypersonbyname" parameterType="java.lang.String" resultType="person">
select id,name,age,birthday,address from Person where name like "%${value}%"
</select> <insert id="inserperson" parameterType="person">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
insert into Person(name,age,address,birthday) value(#{name},#{age},#{address},#{birthday})
</insert> <delete id="deletepersonbyid" parameterType="int">
delete from Person where id = #{id}
</delete> <delete id="updatepersonbyid" parameterType="person">
update Person set name=#{name},age=#{age},birthday=#{birthday},address=#{address} where id = #{id}
</delete> </mapper>
测试:
public class TestMybatis {
public SqlSessionFactory getfactory() throws IOException{
String filepath = "SqlMappingConfig.xml";
InputStream in = Resources.getResourceAsStream(filepath);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
return sqlSessionFactory;
} @Test
public void testinsert() throws IOException{
SqlSessionFactory sqlSessionFactory = this.getfactory();
SqlSession sqlsession = sqlSessionFactory.openSession();
PersonMapper personMapper = sqlsession.getMapper(PersonMapper.class);
Person person = new Person();
person.setName("小白");
person.setAddress("上海");
person.setAge(15);
person.setBirthday("05-04");
personMapper.inserperson(person);
System.out.println("id "+person.getId());
sqlsession.commit();
sqlsession.close();
} @Test
public void testquerybyid() throws IOException{
SqlSessionFactory sqlSessionFactory = this.getfactory();
SqlSession sqlsession = sqlSessionFactory.openSession();
PersonMapper personMapper = sqlsession.getMapper(PersonMapper.class);
Person person = personMapper.querypersonbyid(2);
System.out.println(person);
sqlsession.close();
} @Test
public void testquerybyname() throws IOException{
SqlSessionFactory sqlSessionFactory = this.getfactory();
SqlSession sqlsession = sqlSessionFactory.openSession();
PersonMapper personMapper = sqlsession.getMapper(PersonMapper.class);
List<Person> personlist = personMapper.querypersonbyname("小");
for(int i=0;i<personlist.size();i++){
System.out.println(personlist.get(i));
}
sqlsession.close();
} @Test
public void testdeletebyid() throws IOException{
SqlSessionFactory sqlSessionFactory = this.getfactory();
SqlSession sqlsession = sqlSessionFactory.openSession();
PersonMapper personMapper = sqlsession.getMapper(PersonMapper.class);
personMapper.deletepersonbyid(2);
sqlsession.commit();
sqlsession.close();
} @Test
public void testupdatePerson() throws IOException{
SqlSessionFactory sqlSessionFactory = this.getfactory();
SqlSession sqlsession = sqlSessionFactory.openSession();
PersonMapper personMapper = sqlsession.getMapper(PersonMapper.class);
Person person = new Person();
person.setId(3);
person.setName("老石");
person.setAddress("北京");
person.setAge(15);
person.setBirthday("07-04");
personMapper.updatepersonbyid(person);
sqlsession.commit();
sqlsession.close();
} @Test
public void testquerypersonbyvo() throws IOException{
SqlSessionFactory sqlSessionFactory = this.getfactory();
SqlSession sqlsession = sqlSessionFactory.openSession();
PersonMapper personMapper = sqlsession.getMapper(PersonMapper.class);
PersonVo personVo = new PersonVo();
CustomPerson customPerson = new CustomPerson();
customPerson.setName("老石");
customPerson.setBirthday("07-04");
personVo.setCustomPerson(customPerson);
List<Person> personlist = personMapper.querypersonbyvo(personVo);
for(int i=0;i<personlist.size();i++){
System.out.println(personlist.get(i));
}
sqlsession.close();
}
}
Mybaits入门使用的更多相关文章
- mybaits入门
1.回顾jdbc开发 orm概述 orm是一种解决持久层对象关系映射的规则,而不是一种具体技术.jdbc/dbutils/springdao,hibernate/springorm,mybaits同属 ...
- Mybaits入门之起航
前言 Mybaits技术现在很多公司都在使用,它提供了简单的API可以快速进行数据库操作,所以不管是自己做系统还是找工作都有必要了解一下. 学习一门技术如果是入门的话要么买书要么就是阅读官方的文档,而 ...
- mybaits入门(含实例教程和源码) http://blog.csdn.net/u013142781/article/details/50388204
前言:mybatis是一个非常优秀的存储过程和高级映射的优秀持久层框架.大大简化了,数据库操作中的常用操作.下面将介绍mybatis的一些概念和在eclipse上的实际项目搭建使用. 一.mybati ...
- mybaits入门学习
学习了简单的mybatis的配置 Bean层: 这个都会很简单 一个完整的Bean 需要getter和setter方法还需要一个空的构造方法和一个满的构造方法. Dao层: 创建一个接口就ok了 pa ...
- mybatis入门百分百
今天重新返回来看自己的mybatis,总结了一些更好入门的办法,下面用最简单的方法带领大家入门. 此处先引入类包的关系图片 1.构建一个==普通==maven项目 构建好之后向pom.xml添加一下依 ...
- Mybatis教程(一)
1 Mybatis教程(一) 学习过的持久层框架:DBUtils , Hibernate Mybatis就是类似于hibernate的orm持久层框架. 为什么学Mybatis? 目前最主流 ...
- 深入浅出MyBatis技术原理与实战
第1 章 MyBatis 简介..................................................................................... ...
- MyBaits框架入门总结
MBaits简介 联系方式:18873247271(微信同步) 廖先生 qq:1727292697 MyBatis的前身叫iBatis,本是apache的一个开源项目, 2010年这个项目由apach ...
- My Baits入门(一)mybaits环境搭建
1)在工程下引入mybatis-3.4.1.jar包,再引入数据库(mysql,mssql..)包. 2)在src下新建一个配置文件conf.xml <?xml version="1. ...
随机推荐
- css 图像热区
CSS图像热区 在img标签当中,存在一个属性usemap属性,用来和map标签进行绑定,从而实现图像热区.所谓的图像热区,其实指的是在图像中插入一个连接,当用户点击图像的指定区域后就会进行页面跳转. ...
- 【BZOJ4013】[HNOI2015]实验比较(动态规划)
[BZOJ4013][HNOI2015]实验比较(动态规划) 题面 BZOJ 洛谷 题解 看题目意思就是给你一棵树,连边表示强制顺序关系.然后你要给点染色,在满足顺序关系的情况下,将序列染成若干个颜色 ...
- MySQL架构备份之双机热备
M--S架构:实现双机热备(AB复制) 1.可以降低master读压力 2.可以对数据库做“热备”,热备只能解决硬件master硬件故障,软件故障等重大故障问题,但无法解决人为误操作导致的逻辑故障(列 ...
- [十二省联考2019]异或粽子(堆+可持久化Trie)
前置芝士:可持久化Trie & 堆 类似于超级钢琴,我们用堆维护一个四元组\((st, l, r, pos)\)表示以\(st\)为起点,终点在\([l, r]\)内,里面的最大值的位置为\( ...
- python_unittest学习小结
- (二分查找 拓展) leetcode 162. Find Peak Element && lintcode 75. Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...
- (数组) leetcode 66. Plus One
Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The ...
- IDEA 常用配置
调节代码字体大小 设置:File --- Settings... --- Editor --- General --- 勾选Change font size(Zoom) with Ctrl + Mou ...
- VMware 安装Linux系统 CentOS
VMware 安装Linux系统 CentOS 1. 下载镜像系统 centos镜像下载地址:https://www.centos.org/download/ 选择DVD下载即可 linux各版本下 ...
- DirectX11--实现一个3D魔方(2)
前言 上一章我们主要讲述了魔方的构造和初始化.纹理的准备工作.目前我还没有打算讲Direct3D 11关于底层绘图的实现,因此接下来这一章的重点是魔方的旋转.因为我们要的是能玩的魔方游戏,而不是一个观 ...