我们先来看一个例子,简单的了解一下mybatis的mapper接口方式的使用。

 package org.mybatis.spring.sample;

 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.spring.sample.bean.User;
import org.mybatis.spring.sample.mapper.UserMapper; import java.io.IOException; public class MybatisTest { /**
* 读取mybatis的配置文件,生成SqlSessionFactory
*
* @return
*/
private static SqlSessionFactory getSessionFactory() {
SqlSessionFactory sessionFactory = null;
String resource = "mybatisConfig.xml";
try {
sessionFactory = new SqlSessionFactoryBuilder().build(Resources
.getResourceAsReader(resource));
} catch (IOException e) {
e.printStackTrace();
}
return sessionFactory;
} @Test
public void findUserById() {
SqlSessionFactory sqlSessionFactory = getSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.selectByPrimaryKey(1l);
System.out.println(user.getId() + " / " + user.getName());
} }

输出结果

1 /  赵大

数据库表 user

User.java

 /*
* User.java
* Copyright(C) 2015-2017 Jstudio.org
* All rights reserved.
* --------------------------------------
* 2017-09-17 Created.
*/
package org.mybatis.spring.sample.bean; import java.io.Serializable; /**
*
* This class was generated by MyBatis Generator.
* This class corresponds to the database table user
*
* @mbg.generated do_not_delete_during_merge 2017-09-17
*/
public class User implements Serializable {
/**
* 主键
*/
private Long id; /**
* 用户名
*/
private String name; /**
* 密码
*/
private String password; /**
* 电子邮件
*/
private String email; /**
* 年龄
*/
private Integer age; private static final long serialVersionUID = 1L; /**
* 获取主键
*
* @return id - 主键
*/
public Long getId() {
return id;
} /**
* 设置主键
*
* @param id 主键
*/
public void setId(Long id) {
this.id = id;
} /**
* 获取用户名
*
* @return name - 用户名
*/
public String getName() {
return name;
} /**
* 设置用户名
*
* @param name 用户名
*/
public void setName(String name) {
this.name = name == null ? null : name.trim();
} /**
* 获取密码
*
* @return password - 密码
*/
public String getPassword() {
return password;
} /**
* 设置密码
*
* @param password 密码
*/
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
} /**
* 获取电子邮件
*
* @return email - 电子邮件
*/
public String getEmail() {
return email;
} /**
* 设置电子邮件
*
* @param email 电子邮件
*/
public void setEmail(String email) {
this.email = email == null ? null : email.trim();
} /**
* 获取年龄
*
* @return age - 年龄
*/
public Integer getAge() {
return age;
} /**
* 设置年龄
*
* @param age 年龄
*/
public void setAge(Integer age) {
this.age = age;
}
}

UserMapper.java

 /*
* UserMapper.java
* Copyright(C) 2015-2017 Jstudio.org
* All rights reserved.
* --------------------------------------
* 2017-09-17 Created.
*/
package org.mybatis.spring.sample.mapper; import org.mybatis.spring.sample.bean.User; import java.util.List; public interface UserMapper { int insert(User entity); int insertSelective(User entity); int deleteByPrimaryKey(Long id); int updateByPrimaryKeySelective(User entity); int updateByPrimaryKey(User entity); User selectByPrimaryKey(Long id); }

UserMapper.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="org.mybatis.spring.sample.mapper.UserMapper">
<!-- This is automatically generated by MyBatis Generator on 2017-09-17. -->
<resultMap id="BaseResultMap" type="org.mybatis.spring.sample.bean.User">
<!-- WARNING - This element is automatically generated by MyBatis Generator, modify carefully. -->
<id column="id" jdbcType="BIGINT" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="email" jdbcType="VARCHAR" property="email" />
<result column="age" jdbcType="INTEGER" property="age" />
</resultMap>
<sql id="Base_Column_List">
<!-- WARNING - This element is automatically generated by MyBatis Generator, modify carefully. -->
id, name, password, email, age
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
<!-- WARNING - This element is automatically generated by MyBatis Generator, modify carefully. -->
select
<include refid="Base_Column_List" />
from user
where id = #{id,jdbcType=BIGINT}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
<!-- WARNING - This element is automatically generated by MyBatis Generator, modify carefully. -->
delete from user
where id = #{id,jdbcType=BIGINT}
</delete>
<insert id="insert" parameterType="org.mybatis.spring.sample.bean.User">
<!-- WARNING - This element is automatically generated by MyBatis Generator, modify carefully. -->
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
SELECT LAST_INSERT_ID()
</selectKey>
insert into user (name, password, email,
age)
values (#{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="org.mybatis.spring.sample.bean.User">
<!-- WARNING - This element is automatically generated by MyBatis Generator, modify carefully. -->
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
SELECT LAST_INSERT_ID()
</selectKey>
insert into user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="name != null">
name,
</if>
<if test="password != null">
password,
</if>
<if test="email != null">
email,
</if>
<if test="age != null">
age,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="password != null">
#{password,jdbcType=VARCHAR},
</if>
<if test="email != null">
#{email,jdbcType=VARCHAR},
</if>
<if test="age != null">
#{age,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="org.mybatis.spring.sample.bean.User">
<!-- WARNING - This element is automatically generated by MyBatis Generator, modify carefully. -->
update user
<set>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
<if test="password != null">
password = #{password,jdbcType=VARCHAR},
</if>
<if test="email != null">
email = #{email,jdbcType=VARCHAR},
</if>
<if test="age != null">
age = #{age,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
<update id="updateByPrimaryKey" parameterType="org.mybatis.spring.sample.bean.User">
<!-- WARNING - This element is automatically generated by MyBatis Generator, modify carefully. -->
update user
set name = #{name,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
email = #{email,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER}
where id = #{id,jdbcType=BIGINT}
</update>
</mapper>

mybatisConfig.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> <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://127.0.0.1:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments> <mappers>
<mapper resource="sqlmapping/UserMapper.xml"/>
</mappers>
</configuration>

后继我们将继续讲解Mybatis如何加载配置,解析Mappper的xml,创建和使用Mapper。

最新文章

  1. JAVA 1.5 运算符
  2. linux修改ip地址的方法
  3. 【poj2728】Desert King
  4. &lt;em&gt;标签
  5. button 浏览器兼容问题
  6. Quartz 2D Programming Guide
  7. (八)学习MVC之三级联动
  8. Failed to retrieve procctx from ht. constr
  9. don&#39;t touch your phone in any unfamiliar way(转)
  10. Weblogic的集群
  11. iOS 自动布局过程
  12. 实时监听input标签输入 实时监听文本框输入 避免中文输入法无法触发onkeyup事件的问题
  13. eclipse中maven父子项目层级显示设置
  14. 2.安装以太坊客户端(mac os)
  15. mysql 模糊查询条件带‘%’问题
  16. SSH框架新线程下执行数据库持久化时 No Session found for current thread
  17. TensorFlow函数教程:tf.nn.dropout
  18. python基础学习笔记(六)
  19. 基于快速排序思想partition查找第K大的数或者第K小的数。
  20. Objective-C语法之可变参数

热门文章

  1. js经典试题之w3规范系列
  2. Prime Matrix(暴力出奇迹)
  3. Android 开发错误集锦
  4. C++读取文件统计单词个数及频率
  5. phpshell提权
  6. request.getRequestDispatcher不能实现页面跳转的原因
  7. PAT L2-005 集合相似度
  8. linux系统中如何进入退出vim编辑器的方法及区别
  9. centos 安装mod_wsgi
  10. Java SE1.6中的Synchronized