MyBatis入门(一)---基本使用
一、MyBatis 简介
1.1、概述
MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。
MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。
MyBatis 可以对配置和原生 Map 使用简单的 XML 或注解,将接口和 Java 的 POJOs(Plain Old Java Objects, 普通的 Java 对象) 映射成数据库中的记录。
1.2、ORM
orm 工具的基本思想
无论是用过的 hibernate,mybatis, 你都可以法相他们有一个共同点:
1. 从配置文件 (通常是 XML 配置文件中) 得到 sessionfactory.
2. 由 sessionfactory 产生 session
3. 在 session 中完成对数据的增删改查和事务提交等.
4. 在用完之后关闭 session 。
5. 在 java 对象和 数据库之间有做 mapping 的配置文件,也通常是 xml 文件。
二、环境搭建
2.1、所需要 Jar 包
要使用 MyBatis, 只需将 mybatis-x.x.x.jar 文件置于 classpath 中即可。
2.2、建立数据库与插入数据
这里使用 MySql 数据库:
CREATE TABLE `mybatis`.`user` ( `id` INT(11) NOT NULL AUTO_INCREMENT COMMENT '用户 ID', `name` VARCHAR(45) NOT NULL DEFAULT '无名氏' COMMENT '用户名', `age` TINYINT(3) NOT NULL DEFAULT 21 COMMENT '用户年龄', `birthday` DATETIME NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT '用户生日', `address` VARCHAR(256) NOT NULL DEFAULT '北京' COMMENT '用户地址', PRIMARY KEY (`id`) COMMENT '') COMMENT = '用户表';
insert into user(id,name,age,birthday,address) values(1,'张三',23,'1990-01-23 20:24:21','上海'),(2,'李四',18,'1986-12-23 12:13:11','广州'),(3,'张五',33,'1975-09-23 02:13:11','上海'),(4,'王六',27,'1984-11-01 11:23:14','重庆'),(5,'张三丰',108,'1971-01-02 02:12:11','武当');
2.3、建立 Web 项目把 Jar 包引入项目
三、建立配置文件,实体类,与接口
3.1、建立实体类
/**
@Title: User.java
@Package com.pb.mybatis.po
@Description: TODO(用户类)
@author 刘楠
@date 2015-10-26 下午 5:42:13
@version V1.0
*/
package com.pb.mybatis.po;import java.util.Date;
/**
@ClassName: User
@Description: TODO(用户类)
@author 刘楠
@date 2015-10-26 下午 5:42:13
*/
public class User {</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * id(用户ID) </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> id; </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * name(用户名) </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String name; </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> *age (用户年龄) </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age; </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * birthday(用户生日) </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Date birthday; </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * address (用户地址) </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String address; </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> the id </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> getId() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> id; } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> id the id to set </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setId(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> id) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.id =<span style="color: rgba(0, 0, 0, 1)"> id; } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> the name </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getName() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> name; } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> name the name to set </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setName(String name) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name; } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> the age </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> getAge() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> age; } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> age the age to set </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setAge(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.age =<span style="color: rgba(0, 0, 0, 1)"> age; } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> the brithday </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Date getBirthday() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> brithday; } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> brithday the brithday to set </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setBirthday(Date birthday) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.birthday =<span style="color: rgba(0, 0, 0, 1)"> birthday; } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> the address </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getAddress() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> address; } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> address the address to set </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setAddress(String address) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.address =<span style="color: rgba(0, 0, 0, 1)"> address; }
}
3.2、写实体类接口
/**
@Title: UserMapper.java
@Package com.pb.mybatis.dao
@Description: TODO(用一句话描述该文件做什么)
@author 刘楠
@date 2015-10-26 下午 5:45:13
@version V1.0
*/
package com.pb.mybatis.dao;import com.pb.mybatis.po.User;
/**
@ClassName: UserMapper
@Description: TODO(用户类数据访问接口)
@author 刘楠
@date 2015-10-26 下午 5:45:13
*/
public interface UserMapper {
/**
*
* @Title: selectUserById* @Description: TODO(根据用户ID查询) * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> id * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> User </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> User selectUserById(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> id);
}
3.3、与 db.properties
db.properties
#数据库基本配置 #驱动 driver=com.mysql.jdbc.Driver #连接 url url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8 #用户名 username=root #密码 password=root
3.4、建立映射文件与configuration.xml 配置
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 命名空间要写接口的全路径 --> <mapper namespace="com.pb.mybatis.dao.UserMapper"> <!--ID 要与接口中的方法名相同 parameterType 传入的参数类型 resultType 返回的类型这里也 User 类的全路径--> <select id="selectUserById" parameterType="int" resultType="com.pb.mybatis.po.User"> select * from user where id=#{id} </select> </mapper>
configuration.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> <!-- 加载资源文件 --> <properties resource="db.properties"/> <!--配置环境 --> <environments default="development"> <environment id="development"> <!--事务管理 --> <transactionManager type="JDBC"/> <!--数据源 通过 Properties 加载配置 --> <dataSource type="POOLED"> <!--驱动 driver --> <property name="driver" value="${driver}"/> <!--连接 URL --> <property name="url" value="${url}"/> <!--用户名 --> <property name="username" value="${username}"/> <!--密码 --> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <!--建立映射 --> <mappers> <mapper resource="com/pb/mybatis/dao/UserMapper.xml"/> </mappers> </configuration>
3.5、简单根据 ID 查询
/**
@Title: Test1.java
@Package com.pb.mybatis.test
@Description: TODO(用一句话描述该文件做什么)
@author 刘楠
@date 2015-10-26 下午 5:55:54
@version V1.0
*/
package com.pb.mybatis.test;import java.io.IOException;
import java.io.Reader;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 com.pb.mybatis.dao.UserMapper;
import com.pb.mybatis.po.User;/**
@ClassName: Test1
@Description: TODO(测试类)
@author 刘楠
@date 2015-10-26 下午 5:55:54
*/
public class Test1 {
//Session 工厂
static SqlSessionFactory sqlSessionFactory=null;
//Session
static SqlSession session=null;
//字符流
static Reader reader=null;</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { selectById(); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * * @Title: selectById * @Description: TODO(根据ID查找用户) void </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> selectById(){ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">加载配置文件</span> <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { reader</span>=Resources.getResourceAsReader("configuration.xml"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">建立SqlSessionFactory</span> sqlSessionFactory=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SqlSessionFactoryBuilder().build(reader); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">打开Session</span> session=<span style="color: rgba(0, 0, 0, 1)">sqlSessionFactory.openSession(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取用户接口对象</span> UserMapper userMapper=session.getMapper(UserMapper.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">调用查询方法</span> User user=userMapper.selectUserById(1<span style="color: rgba(0, 0, 0, 1)">); System.out.println(user.getName()</span>+"..."+user.getAge()+"..."+user.getBirthday().toLocaleString()+"..."+<span style="color: rgba(0, 0, 0, 1)">user.getAddress()); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) { e.printStackTrace(); } }
}
3.6、使用别名
<!--使用别名 --> <typeAliases> <!--用户类别名 --> <typeAlias type="com.pb.mybatis.po.User" alias="User"/> </typeAliases>
更改 mapper.xml 中的 User 类的路径 为别名
<?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 命名空间要写接口的全路径 --> <mapper namespace="com.pb.mybatis.dao.UserMapper"> <!--ID 要与接口中的方法名相同 parameterType 传入的参数类型 resultType 返回的类型这里也 User 类的全路径--> <select id="selectUserById" parameterType="int" resultType="User"> select * from user where id=#{id} </select> </mapper>
测试类不变
3.7、使用 resultMap
在 mapper.xml 中使用 resultMap
<?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 命名空间要写接口的全路径 --> <mapper namespace="com.pb.mybatis.dao.UserMapper"><!--结果映射 不区另大小写,建议数据库的列写为大写 -->
<resultMap type="User" id="userMap">
<!--主键 -->
<id property="id" column="ID"/>
<!--姓名 -->
<id property="name" column="NAME"/>
<!--年龄 -->
<id property="age" column="AGE"/>
<!--生日 -->
<id property="birthday" column="BIRTHDAY"/>
<!--地址 -->
<id property="address" column="ADDRESS"/>
</resultMap><!--ID 要与接口中的方法名相同 parameterType 传入的参数类型 resultType 返回的类型这里也 User 类的全路径或者类的别名, resultMap 写已经有的映射 2 都只能同时有一个-->
<select id="selectUserById" parameterType="int" resultMap="userMap">
select * from user
where id=#{id}
</select>
</mapper>
测试类不变
四、实现基本的增、删、改、查
4.1、在接口中增加,模糊查询,添加,修改,删除的方法,
/**
@Title: UserMapper.java
@Package com.pb.mybatis.dao
@Description: TODO(用一句话描述该文件做什么)
@author 刘楠
@date 2015-10-26 下午 5:45:13
@version V1.0
*/
package com.pb.mybatis.dao;import java.util.Date;
import java.util.List;import com.pb.mybatis.po.User;
/**
@ClassName: UserMapper
@Description: TODO(用户类数据访问接口)
@author 刘楠
@date 2015-10-26 下午 5:45:13
*/
public interface UserMapper {
/**
*
* @Title: selectUserById* @Description: TODO(根据用户ID查询) * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> id * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> User </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> User selectUserById(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> id); </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * * @Title: selectUserLikeName * @Description: TODO(根据姓名模糊查询) * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> name * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> List<User> </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> List<User><span style="color: rgba(0, 0, 0, 1)"> selectUserLikeName(String name); </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * * @Title: addUser * @Description: TODO(添加用户) * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> user void </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> addUser(User user); </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * * @Title: updateUser * @Description: TODO(修改用户) * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> user void </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> updateUser(User user); </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * * @Title: deleteUser * @Description: TODO(删除用户) * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> id void </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> deleteUser(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> id);
}
4.2、在 mapper.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 命名空间要写接口的全路径 --> <mapper namespace="com.pb.mybatis.dao.UserMapper"><!--结果映射 不区另大小写,建议数据库的列写为大写 -->
<resultMap type="User" id="userMap">
<!--主键 -->
<id property="id" column="ID"/>
<!--姓名 -->
<id property="name" column="NAME"/>
<!--年龄 -->
<id property="age" column="AGE"/>
<!--生日 -->
<id property="birthday" column="BIRTHDAY"/>
<!--地址 -->
<id property="address" column="ADDRESS"/>
</resultMap><!--ID 要与接口中的方法名相同 parameterType 传入的参数类型 resultType 返回的类型这里也 User 类的全路径或者类的别名, resultMap 写已经有的映射 2 都只能同时有一个-->
<select id="selectUserById" parameterType="int" resultMap="userMap">
select * from user
where id=#{id}
</select>
<!--根据姓名模糊查询 -->
<select id="selectUserLikeName" parameterType="String" resultMap="userMap">
select * from user
where name like "%"#{name}"%"
</select><!--添加用户 useGeneratedKeys 使用数据的增序列 keyProperty 将增加后的用户 ID 返回-->
<insert id="addUser" parameterType="User" useGeneratedKeys="true" keyProperty="id">
insert into user(name,age,birthday,address)
values(#{name},#{age},#{birthday},#{address})
</insert>
<!--修改更新 -->
<update id="updateUser" parameterType="User">
update user set name=#{name},age=#{age},birthday=#{birthday},address=#{address}
where id=#{id}
</update>
<!--删除 -->
<delete id="deleteUser" parameterType="int">
delete from user
where id=#{id}
</delete>
</mapper>
4.3、测试类
/**
@Title: Test1.java
@Package com.pb.mybatis.test
@Description: TODO(用一句话描述该文件做什么)
@author 刘楠
@date 2015-10-26 下午 5:55:54
@version V1.0
*/
package com.pb.mybatis.test;import java.io.IOException;
import java.io.Reader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
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 com.pb.mybatis.dao.UserMapper;
import com.pb.mybatis.po.User;/**
- @ClassName: Test1
- @Description: TODO(测试类)
- @author 刘楠
- @date 2015-10-26 下午 5:55:54
*/
public class Test1 {
// Session 工厂
static SqlSessionFactory sqlSessionFactory = null;
// Session
static SqlSession session = null;
// 字符流
static Reader reader = null;</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * * @Title: selectById * * @Description: TODO(根据ID查找用户) void </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> selectById() { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 加载配置文件</span> <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { reader </span>= Resources.getResourceAsReader("configuration.xml"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 建立SqlSessionFactory</span> sqlSessionFactory = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SqlSessionFactoryBuilder().build(reader); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打开Session</span> session =<span style="color: rgba(0, 0, 0, 1)"> sqlSessionFactory.openSession(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取用户接口对象</span> UserMapper userMapper = session.getMapper(UserMapper.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 调用查询方法</span> User user = userMapper.selectUserById(1<span style="color: rgba(0, 0, 0, 1)">); System.out.println(user.getName() </span>+ "..." + user.getAge() + "..." + user.getBirthday().toLocaleString() + "..." +<span style="color: rgba(0, 0, 0, 1)"> user.getAddress()); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) { e.printStackTrace(); } } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * * @Title: selectLikeName * * @Description: TODO(根据用户名模糊查询) void </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> selectLikeName() { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 加载配置文件</span> <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { reader </span>= Resources.getResourceAsReader("configuration.xml"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 建立SqlSessionFactory</span> sqlSessionFactory = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SqlSessionFactoryBuilder().build(reader); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打开Session</span> session =<span style="color: rgba(0, 0, 0, 1)"> sqlSessionFactory.openSession(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取用户接口对象</span> UserMapper userMapper = session.getMapper(UserMapper.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 调用查询方法</span> List<User> users = userMapper.selectUserLikeName("张"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (User user : users) { System.out.println(user.getName() </span>+ "..." +<span style="color: rgba(0, 0, 0, 1)"> user.getAge() </span>+ "..." + user.getBirthday().toLocaleString() + "..." +<span style="color: rgba(0, 0, 0, 1)"> user.getAddress()); } } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) { e.printStackTrace(); } } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * * @Title: addUser * @Description: TODO(添加用户) void </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> addUser() { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 加载配置文件</span> <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { reader </span>= Resources.getResourceAsReader("configuration.xml"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 建立SqlSessionFactory</span> sqlSessionFactory = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SqlSessionFactoryBuilder().build(reader); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打开Session</span> session =<span style="color: rgba(0, 0, 0, 1)"> sqlSessionFactory.openSession(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取用户接口对象</span> UserMapper userMapper = session.getMapper(UserMapper.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 声明新用户</span> User user =<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> User(); user.setName(</span>"呵呵"<span style="color: rgba(0, 0, 0, 1)">); user.setAge(</span>22<span style="color: rgba(0, 0, 0, 1)">); String d</span>="1984-09-23 20:23:22"<span style="color: rgba(0, 0, 0, 1)">; SimpleDateFormat sdf</span>=<span style="color: rgba(0, 0, 255, 1)">new</span> SimpleDateFormat("yyyy-MM-DD HH🇲🇲ss"<span style="color: rgba(0, 0, 0, 1)">); Date date</span>=<span style="color: rgba(0, 0, 0, 1)">sdf.parse(d); user.setBirthday(date); user.setAddress(</span>"不知道是哪的"<span style="color: rgba(0, 0, 0, 1)">); userMapper.addUser(user); System.out.println(</span>"插入后的ID"+<span style="color: rgba(0, 0, 0, 1)">user.getId()); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">提交事务</span>
session.commit();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}} </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * * @Title: updateUser * @Description: TODO(修改用户) void </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> updateUser() { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 加载配置文件</span> <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { reader </span>= Resources.getResourceAsReader("configuration.xml"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 建立SqlSessionFactory</span> sqlSessionFactory = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SqlSessionFactoryBuilder().build(reader); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打开Session</span> session =<span style="color: rgba(0, 0, 0, 1)"> sqlSessionFactory.openSession(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取用户接口对象</span> UserMapper userMapper = session.getMapper(UserMapper.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 调用查询方法</span> User user = userMapper.selectUserById(6<span style="color: rgba(0, 0, 0, 1)">); user.setName(</span>"想起 来叫什么了"<span style="color: rgba(0, 0, 0, 1)">); user.setAddress(</span>"魔都上海"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">更新</span>
userMapper.updateUser(user);
//提交事务
session.commit();
} catch (IOException e) {
e.printStackTrace();
}} </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> detleUser() { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 加载配置文件</span> <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { reader </span>= Resources.getResourceAsReader("configuration.xml"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 建立SqlSessionFactory</span> sqlSessionFactory = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SqlSessionFactoryBuilder().build(reader); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打开Session</span> session =<span style="color: rgba(0, 0, 0, 1)"> sqlSessionFactory.openSession(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取用户接口对象</span> UserMapper userMapper = session.getMapper(UserMapper.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除</span> userMapper.deleteUser(6<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">提交事务</span>
session.commit();
} catch (IOException e) {
e.printStackTrace();
}}
}