一、输入映射
通过parameterType指定输入参数类型,类型可以是简单类型、HashMap、pojo的包装类型。
1、简单类型
映射文件:
<!--
查询sql语句配置使用select标签
id:该语句的唯一标识,通常称为statement的id
parameterType:输入参数的类型
resultType:返回数据的类型,指定为Java的po类型,则将查询出来的单条记录映射为po对象。
-->
<select id="findUserById" parameterType="int" resultType="com.jack.po.User" >
<!--
要执行的sql语句
#{} :表示一个占位符
#{value} :value表示接受的参数,名称为value,如果参数是简单类型,则名称可以随意起。
-->
SELECT * FROM user WHERE id=#{value}
</select>
接口文件:
//根据id查询用户的接口
public User findUserById(int id) throws Exception;
测试文件:
public void findUserById(){
//创建SqlSession对象
SqlSession sqlSession = sessionFactory.openSession();
try {
/**
* 执行SqlSession对象的方法
* 第一个参数为:映射文件中的namespace+.+statement id
* 第二个参数为:要传入的参数值
*/
User user = sqlSession.selectOne("test.findUserById",1);
System.out.println(user);
} catch (Exception e) {
e.printStackTrace();
}finally {
}
}
2、HashMap类型
映射文件:
<select id="findUserByHm" parameterType="java.util.HashMap" resultType="com.jack.po.User" >
<!--
要执行的sql语句
#{} :表示一个占位符
#{id} :id表示HashMap中的key
-->
SELECT * FROM user WHERE id=#{id}
</select>
接口文件:
//根据id查询用户的接口
public User findUserByHm(HashMap hashMap) throws Exception;
测试文件:
public void findUserByHm(){
SqlSession sqlSession = sessionFactory.openSession();
try {
HashMap<String,Integer> hashMap = new HashMap<>();
hashMap.put("id",10);
User user = sqlSession.selectOne("test.findUserByHm",hashMap);
System.out.println(user);
} catch (Exception e) {
e.printStackTrace();
}finally {
}
}
3、pojo类型:###
pojo文件:
public class UserVo {
private User user;
private Shop shop;
@Override
public String toString() {
return "UserVo{" +
"user=" + user +
", shop=" + shop +
'}';
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Shop getShop() {
return shop;
}
public void setShop(Shop shop) {
this.shop = shop;
}
}
映射文件:
<select id="findUserByShop" parameterType="com.jack.po.UserVo" resultType="com.jack.po.User">
<!--
#{}中的值为parameterType类型中属性对象中的属性值
-->
SELECT t1.* FROM user t1,shop t2 WHERE t1.id = t2.user_id AND t2.sid = #{shop.id}
</select>
接口文件:
//根据商品id查询用户的接口
public User findUserByShop(UserVo userVo) throws Exception;
测试文件:
public void findUserByShop(){
SqlSession sqlSession = sessionFactory.openSession();
try {
UserVo userVo = new UserVo();
Shop shop = new Shop();
shop.setId(1);
userVo.setShop(shop);
User user = sqlSession.selectOne("test.findUserByShop",userVo);
System.out.println(user);
} catch (Exception e) {
e.printStackTrace();
}finally {
}
}
二、输出映射
通过resultType指定输出参数类型,只有查询出来的列名和pojo中的属性名一致时,该列才可以和Java对象映射成功。
如果查询出来的列名和pojo中的属性名称全部不一致,则不会创建pojo对象。
只有查询出来的列名和pojo中的属性有一个一致,就会创建pojo对象。
如果查询出来的列名中有和属性名称不一致的,则该列不会和pojo的属性进行映射。
1、简单类型
映射文件:
<select id="findUserCountByName" parameterType="String" resultType="int">
SELECT count(1) FROM user WHERE username LIKE '%${value}%'
</select>
接口文件:
//根据用户名查询用户数量
public int findUserCountByName(String name) throws Exception;
测试文件:
public void findUserCountByName(){
SqlSession sqlSession = sessionFactory.openSession();
try {
int count = sqlSession.selectOne("test.findUserCountByName","小明");
System.out.println(count);
} catch (Exception e) {
e.printStackTrace();
}finally {
}
}
2、pojo类型
映射文件:
<select id="findUserByName" parameterType="String" resultType="com.jack.po.User">
SELECT * FROM user WHERE username LIKE '%${value}%'
</select>
接口文件:
//根据用户名查询用户
public List<User> findUserByName(String name) throws Exception;
测试文件:
public void findUserByName(){
SqlSession sqlSession = sessionFactory.openSession();
try {
List<User> list = sqlSession.selectList("test.findUserByName","小明");
for (User user:list) {
System.out.println(user);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
}
}
3、resultMap类型
pojo文件:
public class UserVo {
private User user;
private Shop shop;
@Override
public String toString() {
return "UserVo{" +
"user=" + user +
", shop=" + shop +
'}';
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Shop getShop() {
return shop;
}
public void setShop(Shop shop) {
this.shop = shop;
}
}
映射文件:
<!--
id:resultmap的唯一标识。
type:返回的POJO结果类型
-->
<resultMap id="VoMap" type="com.jack.po.UserVo">
<!--
id表示查询结果中的唯一标识
column:查询出来的列名
property:POJO中的对象属性名称
-->
<id column="id" property="user.id" />
<!--
result表示查询结果中的普通列
column:查询出来的列名
property:POJO中的对象属性名称
-->
<result column="username" property="user.username"/>
<result column="birthday" property="user.birthday"/>
<result column="sex" property="user.sex"/>
<result column="address" property="user.address"/>
<result column="sid" property="shop.sid"/>
<result column="name" property="shop.name"/>
<result column="price" property="shop.price"/>
</resultMap>
<select id="findVoByShop" parameterType="com.jack.po.UserVo" resultMap="VoMap">
<!--
#{}中的值为parameterType类型中属性对象中的属性值
-->
SELECT t1.* ,t2.* FROM user t1,shop t2 WHERE t1.id = t2.user_id AND t1.id = #{user.id}
</select>
接口文件:
//根据商品id查询用户和商品的接口
public List<UserVo> findVoByShop(UserVo userVo) throws Exception;
测试文件:
public void findVoByShop(){
SqlSession sqlSession = sessionFactory.openSession();
try {
UserVo userVo = new UserVo();
User user = new User();
user.setId(10);
userVo.setUser(user);
List<UserVo> userVo1 = sqlSession.selectList("test.findVoByShop",userVo);
for (UserVo uservo: userVo1
) {
System.out.println(uservo);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
}
}