特殊情况示栗:
修改实体类
package com.neusoft.domain;
import java.io.Serializable;
import java.util.Date;
/**
* @author Eric Lee
* @date 2020/9/3 09:45
*/
public class User implements Serializable {
private Integer userId;
private String userName;
private Date userBirthday;
private String userSex;
private String userAddress;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Date getUserBirthday() {
return userBirthday;
}
public void setUserBirthday(Date userBirthday) {
this.userBirthday = userBirthday;
}
public String getUserSex() {
return userSex;
}
public void setUserSex(String userSex) {
this.userSex = userSex;
}
public String getUserAddress() {
return userAddress;
}
public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", userName='" + userName + '\'' +
", userBirthday=" + userBirthday +
", userSex='" + userSex + '\'' +
", userAddress='" + userAddress + '\'' +
'}';
}
}
我们运行 findAll() 查询结果如下
查出上面结果的原因是数据库字段与实体类没有对应, usename可以封装原因是window系统mysql数据不区分大小写
解决方案1查询时候起别名
<!-- 查询所有-->
<select id="findAll" resultType="com.neusoft.domain.User">
select id as userId, username as userName, sex as userSex , birthday as userBirthday, address as userAddress from user
</select>
解决方案2 使用resultMap
<!-- 配置查询结果的列名和实体属性名的对应关系-->
<!-- id 给定一个唯一标识, 是给select标签引用用的-->
<!-- type是指 实体类的全限定类名-->
<resultMap id="userMap" type="com.neusoft.domain.User">
<!-- id标签 主键字段的对应-->
<id property="userId" column="id"></id>
<!-- result标签 用于指定非主键 , column是数据库中列名-->
<!-- property 用于指定实体类属性名名称-->
<result property="userName" column="username"></result>
<result property="userAddress" column="address"></result>
<result property="userSex" column="sex"></result>
<result property="userBirthday" column="birthday"></result>
</resultMap>
完整的IUserDao.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="com.neusoft.dao.IUserDao">
<!-- 配置查询结果的列名和实体属性名的对应关系-->
<!-- id 给定一个唯一标识, 是给select标签引用用的-->
<!-- type是指 实体类的全限定类名-->
<resultMap id="userMap" type="com.neusoft.domain.User">
<!-- id标签 主键字段的对应-->
<id property="userId" column="id"></id>
<!-- result标签 用于指定非主键 , column是数据库中列名-->
<!-- property 用于指定实体类属性名名称-->
<result property="userName" column="username"></result>
<result property="userAddress" column="address"></result>
<result property="userSex" column="sex"></result>
<result property="userBirthday" column="birthday"></result>
</resultMap>
<!--<!– 查询所有–>-->
<!-- <select id="findAll" resultType="com.neusoft.domain.User">-->
<!-- select id as userId, username as userName, sex as userSex , birthday as userBirthday, address as userAddress from user-->
<!-- </select>-->
<!-- 查询所有-->
<!-- <select id="findAll" resultType="com.neusoft.domain.User">-->
<select id="findAll" resultMap="userMap">
select * from user
</select>
<!--<!– 通过id进行查询–>-->
<!-- <select id="findById" parameterType="INT" resultType="com.neusoft.domain.User">-->
<select id="findById" parameterType="INT" resultMap="userMap">
select * from user where id = #{uid}
</select>
<!-- 保存用户-->
<!-- savaUser-->
<insert id="savaUser" parameterType="com.neusoft.domain.User">
-- 配置保存时获取插入id keyColumn数据库中的列名 keyProperty实体类
<selectKey keyColumn="id" keyProperty="userId" resultType="INT">
select last_insert_id();
</selectKey>
insert into user (username, birthday, sex, address)
values (#{userName},#{userBirthday} ,#{userSex},#{userAddress})
</insert>
<!-- 更新用户-->
<update id="updateUser" parameterType="com.neusoft.domain.User">
update user set username = #{userName},birthday=#{userBirthday},
sex=#{userSex}, address=#{userAddress} where id = #{userId}
</update>
<!--删除用户-->
<delete id="deleteUser" parameterType="java.lang.Integer">
delete from user where id = #{id}
</delete>
<!-- 根据姓名进行模糊查询-->
<!-- <select id="findByName" parameterType="java.lang.String" resultType="com.neusoft.domain.User">-->
<select id="findByName" parameterType="java.lang.String" resultMap="userMap">
select * from user where username like #{username};
</select>
<!-- 查询总记录数-->
<select id="findTotal" resultType="java.lang.Integer">
select count(*) from user;
</select>
</mapper>
面试题 #{} 与${} 区别
{} 表示一个占位符号
通过#{}可以实现 preparedStatement 向占位符中设置值,自动进行 java 类型和 jdbc 类型转换,
{}可以有效防止 sql 注入。 #{}可以接收简单类型值或 pojo 属性值。 如果 parameterType 传输单个简单类型值,#{}括号中可以是 value 或其它名称。
{}可以将 parameterType 传入的内容拼接在 sql中且不进行 jdbc 类型转换, {}括号中只能是 value
在sqlmapconfigure.xml中配置参数别名
<typeAliases>
<!-- 单个类起别名-->
<!-- <typeAlias type="com.neusoft.domain.User" alias="user"></typeAlias>-->
<!-- 配置起别名的包 当指定之后, 该包下的实体类都会注册别名,并且类名就是别名,不区分大小写-->
<package name="com.neusoft.domain"/>
</typeAliases>
对应的IUserDao.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="com.neusoft.dao.IUserDao">
<!-- 配置查询结果的列名和实体属性名的对应关系-->
<!-- id 给定一个唯一标识, 是给select标签引用用的-->
<!-- type是指 实体类的全限定类名-->
<resultMap id="userMap" type="user">
<!-- id标签 主键字段的对应-->
<id property="userId" column="id"></id>
<!-- result标签 用于指定非主键 , column是数据库中列名-->
<!-- property 用于指定实体类属性名名称-->
<result property="userName" column="username"></result>
<result property="userAddress" column="address"></result>
<result property="userSex" column="sex"></result>
<result property="userBirthday" column="birthday"></result>
</resultMap>
<!--<!– 查询所有–>-->
<!-- <select id="findAll" resultType="com.neusoft.domain.User">-->
<!-- select id as userId, username as userName, sex as userSex , birthday as userBirthday, address as userAddress from user-->
<!-- </select>-->
<!-- 查询所有-->
<!-- <select id="findAll" resultType="com.neusoft.domain.User">-->
<select id="findAll" resultMap="userMap">
select * from user
</select>
<!--<!– 通过id进行查询–>-->
<!-- <select id="findById" parameterType="INT" resultType="com.neusoft.domain.User">-->
<select id="findById" parameterType="INT" resultMap="userMap">
select * from user where id = #{uid}
</select>
<!-- 保存用户-->
<!-- savaUser-->
<insert id="savaUser" parameterType="user">
-- 配置保存时获取插入id keyColumn数据库中的列名 keyProperty实体类
<selectKey keyColumn="id" keyProperty="userId" resultType="INT">
select last_insert_id();
</selectKey>
insert into user (username, birthday, sex, address)
values (#{userName},#{userBirthday} ,#{userSex},#{userAddress})
</insert>
<!-- 更新用户-->
<update id="updateUser" parameterType="user">
update user set username = #{userName},birthday=#{userBirthday},
sex=#{userSex}, address=#{userAddress} where id = #{userId}
</update>
<!--删除用户-->
<delete id="deleteUser" parameterType="java.lang.Integer">
delete from user where id = #{id}
</delete>
<!-- 根据姓名进行模糊查询-->
<!-- <select id="findByName" parameterType="java.lang.String" resultType="com.neusoft.domain.User">-->
<!-- select * from user where username like '%${value}%';
-->
<select id="findByName" parameterType="java.lang.String" resultMap="userMap">
select * from user where username like #{username};
</select>
<!-- 查询总记录数-->
<select id="findTotal" resultType="java.lang.Integer">
select count(*) from user;
</select>
</mapper>