首页 技术 正文
技术 2022年11月6日
0 收藏 609 点赞 918 浏览 6796 个字

我们先来看一个例子,简单的了解一下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

MyBatis框架的使用及源码分析(一)     配置与使用

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。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291