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入门使用的更多相关文章

  1. mybaits入门

    1.回顾jdbc开发 orm概述 orm是一种解决持久层对象关系映射的规则,而不是一种具体技术.jdbc/dbutils/springdao,hibernate/springorm,mybaits同属 ...

  2. Mybaits入门之起航

    前言 Mybaits技术现在很多公司都在使用,它提供了简单的API可以快速进行数据库操作,所以不管是自己做系统还是找工作都有必要了解一下. 学习一门技术如果是入门的话要么买书要么就是阅读官方的文档,而 ...

  3. mybaits入门(含实例教程和源码) http://blog.csdn.net/u013142781/article/details/50388204

    前言:mybatis是一个非常优秀的存储过程和高级映射的优秀持久层框架.大大简化了,数据库操作中的常用操作.下面将介绍mybatis的一些概念和在eclipse上的实际项目搭建使用. 一.mybati ...

  4. mybaits入门学习

    学习了简单的mybatis的配置 Bean层: 这个都会很简单 一个完整的Bean 需要getter和setter方法还需要一个空的构造方法和一个满的构造方法. Dao层: 创建一个接口就ok了 pa ...

  5. mybatis入门百分百

    今天重新返回来看自己的mybatis,总结了一些更好入门的办法,下面用最简单的方法带领大家入门. 此处先引入类包的关系图片 1.构建一个==普通==maven项目 构建好之后向pom.xml添加一下依 ...

  6. Mybatis教程(一)

    1      Mybatis教程(一) 学习过的持久层框架:DBUtils , Hibernate Mybatis就是类似于hibernate的orm持久层框架. 为什么学Mybatis? 目前最主流 ...

  7. 深入浅出MyBatis技术原理与实战

    第1 章 MyBatis 简介..................................................................................... ...

  8. MyBaits框架入门总结

    MBaits简介 联系方式:18873247271(微信同步) 廖先生 qq:1727292697 MyBatis的前身叫iBatis,本是apache的一个开源项目, 2010年这个项目由apach ...

  9. My Baits入门(一)mybaits环境搭建

    1)在工程下引入mybatis-3.4.1.jar包,再引入数据库(mysql,mssql..)包. 2)在src下新建一个配置文件conf.xml <?xml version="1. ...

随机推荐

  1. python学习日记(正则表达式)

    定义 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. re 模块使 Pyth ...

  2. 【MyBatis】Mapper XML 文件

    Mapper XML文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立 ...

  3. yii2 gridview默认排序

    Yii2 GridView 使用起来很方便,但是默认排序很是个问题,数据默认按 主键 正序排列 但是在使用过程中,大多数数据默认是 倒序才符合正常思维的. 第一次 的解决方法是在 直接为 Model添 ...

  4. 【CH2401】送礼物

    题目大意:NPC 子集和问题. 题解:先搜索一半的物品重量和,记录在一个数组中,并将该数组排序并去重.再搜索另一半物品,到达目标状态后,在前一半物品记录的重量中查找小于当前剩余重量的最大值,更新答案即 ...

  5. Qt(MinGW版)在win7 64位上无法播放视频解决方案

    [原因分析] Qt自带的MinGW是32位版本,不支持64位的ffmpeg(解码器). 无法播放视频,问题就出在opencv_ffmpeg2411_64.dll(opencv\bin\)上. [解决方 ...

  6. [面试] Java GC (未整理完)

    Java GC简介 什么是 GC ? Java程序不用像C++程序在程序中自行处理内存的回收释放.这是因为Java在JVM虚拟机上增加了垃圾回收(GC)机制,用以在合适的时间触发垃圾回收. 你都了解哪 ...

  7. Spring Cloud中关于Feign的常见问题总结

    一.FeignClient接口,不能使用@GettingMapping 之类的组合注解 代码示例: @FeignClient("microservice-provider-user" ...

  8. Nginx 完全配置

    入门教程 初识Nginx 你真的了解如何将 Nginx 配置为Web服务器吗 设置静态网页编码 --> 针对非类Unix系统 针对服务器 http { ... charset UTF-8; .. ...

  9. 新版本的Python问题

    1.在print方面,新版本需要加括号,调用函数时也是如此,比如: import string s='the quick brown fox jumped to the lazy dog' print ...

  10. CSS3 Flex布局

    Flex 用于使页面上的内容自适应屏幕 首先,在网页代码的头部,加入一行viewport元标签. <meta name=”viewport” content=”width=device-widt ...