视频来源:http://www.icoolxue.com/album/show/216
- 工程搭建
1.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:3306/mybatis?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="0804" />
</dataSource>
</environment>
</environments>
<!-- 加载映射文件 -->
<mappers>
<mapper resource="sqlmap/User.xml"/>
</mappers>
</configuration>
2.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">
<!-- namespace命名空间,作用就是对sql进行分类化管理,理解sql隔离
注意:使用mapper代理方法开发,namespace有特殊重要的作用
-->
<mapper namespace="test">
<!-- 在影射文件中配置很多sql语句 -->
<!-- 需求:通过id查询用户表的记录 -->
<!-- 通过 select 孩子能够数据库查询
将sql语句封装到mapperStatement对象中,所以将id称为statement的id
-->
<!--
parameterType:指定输入参数的类型 findUserById(int id) id的类型是int
#{}表示一个占位符
#{id}: 其中id接收输入的参数,蚕食名称就是id,如果输入的参数是简单类型,#{}中的参数可以任意,可以value或者其他名称
resultType:指定sql输出结果的所映射的java对象类型,select指定resultType表示单条记录映射的java类型
-->
<select id="findUserById" parameterType="int" resultType="cn.whx.mybatis.po.User">
SELECT * FROM USER WHERE ID=#{id}
</select>
<!-- 查询所有数据,但是不包括id -->
<select id="findAllUser" resultType="map">
SELECT * FROM USER
</select>
<!-- 通过配置resultMap来获取所有的数据,包括id;而且User属性与表中的字段不一致可以通过property和column来配置 -->
<resultMap type="cn.whx.mybatis.po.User" id="userResultMap">
<id property="id" column="id"></id>
<result property="username" column="username"/>
<result property="sex" column="sex"/>
<result property="address" column="address"/>
<result property="birthday" column="birthday"/>
</resultMap>
<select id="findAllByResultMap" resultMap="userResultMap">
select * from user
</select>
</mapper>
3.User.java
package cn.whx.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 + ", sex=" + sex + ", birthday=" + birthday + ", address="
+ address + "]";
}
}
4.测试 根据id查询,和查询所有包括,类属性与表字段不同需要配置resultMap输出
List<Map<String, Object>> 和List<User>
package cn.whx.mybatis.first;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
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.whx.mybatis.po.User;
public class mybatisFirst {
//根据id查询用户信息,得到一条记录的结果
@Test
public void findUserByIdTset() throws IOException{
//mybatis配置文件
String resource = "SqlMapConfig.xml";
//得到配置文件流
InputStream inputStream = Resources.getResourceAsStream(resource);
//创建会话工厂,传入mybatis的配置文件
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//通过工厂得到SQLSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//通过SqlSession操作数据库
/*
* 第一个参数:映射文件中statement的id,等于=namespace+'.'+statement的id
* 第二个参数:指定和映射文件中所匹配的映射的parameterType类型的参数
* sqlSession.selectOne结果是与映射文件中所匹配的resultType类型的对象
*/
User user = sqlSession.selectOne("test.findUserById",1);
List<Map<String, Object>> list = sqlSession.selectList("test.findAllUser");
System.out.println(user);
System.out.println("=====================");
for (Map<String, Object> map : list) {
System.out.println(map);
}
System.out.println("=============");
List<User> list2 = sqlSession.selectList("test.findAllByResultMap");
for (User user2 : list2) {
System.out.println(user2);
}
//关闭Session
sqlSession.close();
}
}
5.输出结果