SqlMapConfig.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>
<!-- 和spring整合后 environments配置将废除-->
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理-->
<transactionManager type="JDBC" />
<!-- 数据库连接池-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:8010/test?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="wazl2012" />
</dataSource>
</environment>
</environments>
<!-- 加载映射文件 -->
<mappers>
<mapper resource="sqlmap/User.xml"/>
</mappers>
</configuration>
User.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="test">
<!--
需求:根据id获取用户信息
id:表示表示映射文件中的sql,将sql语句封装到mappedStatement对象中,将id称为Statement的 id
parameterType:指定出入参数类型
resultType:指定输出参数类型
#{}表示一个占位符
#{id}接收输入参数,参数名为id,如果输入的是简单类型,#{}中的参数名可以任意
-->
<select id="findUserById" parameterType="int" resultType="cn.ztc.mybatis.po.User">
select * from user where id = #{id}
</select>
<!--
需求:自定义条件查询用户列表 ,可能返回多条数据
resultType:指定的就是单条记录所使用的java对象类型
${}表示拼接sql串,将接收到的参数不加任何修饰拼接在sql串中
使用${}可能会引起sqk注入
${value}接收输入参数,如果输入的是简单类型,#{}中的参数名只能是value
-->
<select id="findUserByName" parameterType="java.lang.String" resultType="cn.ztc.mybatis.po.User">
select * from user where username like '%${value}%'
</select>
<!--
添加用户
parameterType:输入参数类型是pojo
#{}中指定pojo的属性名
-->
<insert id="insertUser" parameterType="cn.ztc.mybatis.po.User">
<!--
将插入数据库的主键返回到对象中
select LAST_INSERT_ID():得到insert记录的主键值,只适用于自增主键
keyProperty:将查询到的主键值设置到parameterType指定对象的某个属性
order:相对于insert语句来说,select LAST_INSERT_ID()的执行顺序
-->
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
select LAST_INSERT_ID()
</selectKey>
insert into user (username,birthday,sex,address) values (#{username},#{birthday},#{sex},#{address})
<!--
使用mysql的uuid生成主键
执行过程:
首先用过uuid()得到主键,将主键设置到user对象的id属性中
其次在执行insert,从user对象中取出id属性值
-->
<!-- <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String">
select uuid()
</selectKey>
insert into user (id,username,birthday,sex,address) values (#{id},#{username},#{birthday},#{sex},#{address}) -->
</insert>
<!-- 删除用户 -->
<delete id="deleteUser" parameterType="java.lang.Integer">
delete from user where id=#{id}
</delete>
<!--
更新用户
#{id}:对应pojo的属性值
-->
<update id="updateUser" parameterType="cn.ztc.mybatis.po.User">
update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id = #{id}
</update>
</mapper>
User.java
package cn.ztc.mybatis.po;
import java.util.Date;
public class User {
private int id;
private String username;
private String sex;
private Date birthday;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + "]";
}
}
MybatisFirst.java
package cn.ztc.mybatis.first;
import java.io.IOException;
import java.io.InputStream;
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 org.junit.Test;
import cn.ztc.mybatis.po.User;
public class MybatisFirst {
@Test
public void findUserByIdTest() throws IOException{
String resource = "SqlMapConfig.xml";
//得到配置文件流
InputStream inputStream = Resources.getResourceAsStream(resource);
//创建会化工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//通过sqlSession操作数据库
//第一个参数:映射文件中的[namespace].[statement id]
//第二个参数:映射文件中匹配的resultType类型的对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//selectOne:查一条记录
User user = sqlSession.selectOne("test.findUserById", 1);
System.out.println(user);
sqlSession.close();
}
@Test
public void findUserByNameTest() throws IOException {
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
List<User> list = sqlSession.selectList("test.findUserByName", "小明");
System.out.println(list);
sqlSession.close();
}
@Test
public void insertUserTest() throws IOException {
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
User user = new User();
user.setUsername("王小军");
user.setBirthday(new Date());
user.setSex("1");
user.setAddress("上海");
sqlSession.insert("test.insertUser", user);
sqlSession.commit();
System.out.println(user.getId());
sqlSession.close();
}
@Test
public void deleteUserTest() throws IOException {
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
sqlSession.delete("test.deleteUser", 27);
sqlSession.commit();
sqlSession.close();
}
@Test
public void updateUserTest() throws IOException {
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
User user = new User();
user.setId(24);
user.setUsername("张四丰");
user.setBirthday(new Date());
user.setSex("1");
user.setAddress("上海");
sqlSession.update("test.updateUser", user);
sqlSession.commit();
sqlSession.close();
}
}