parameterType和resultType
parameterType指定输入参数的java类型,可以填写别名或Java类的全限定名。
resultType指定输出结果的java类型,可以填写别名或Java类的全限定名。
#{}和${}
#{}:相当于预处理中的占位符?。
#{}里面的参数表示接收java输入参数的名称。
#{}可以接受HashMap、POJO类型的参数。
当接受简单类型的参数时,#{}里面可以是value,也可以是其他。
#{}可以防止SQL注入。
$ {}:相当于拼接SQL串,对传入的值不做任何解释的原样输出。
$ {} 会引起SQL注入,所以要谨慎使用。
$ {} 可以接受HashMap、pojo类型的参数。
当接受简单类型的参数时,${}里面只能是value。
selectOne和selectList
selectOne:只能查询0或1条记录,大于1条记录的话,会报错:
selectList:可以查询0或N条记录
mapper代理方式实现MyBatis的Dao编写
mapper接口的全限定名要和mapper映射文件的namespace的值相同。
mapper接口的方法名称要和mapper映射文件中的statement的id相同;
mapper接口的方法参数只能有一个,且类型要和mapper映射文件中statement的parameterType的值保持一致。
mapper接口的返回值类型要和mapper映射文件中statement的resultType值或resultMap中的type值保持一致;
db.properties
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/spring_day04
username=root
password=123456
mybatis-config.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"/>
<typeAliases>
<!--自定义别名-->
<typeAlias type="com.hetl.day01.pojo.User" alias="user"/>
</typeAliases>
<!--配置mybatis环境变量-->
<environments default="development">
<environment id="development">
<!--配置jdbc事务管理-->
<transactionManager type="JDBC"></transactionManager>
<!--配置dataSource-->
<dataSource type="POOLED">
<property name="driver" value="${driverClass}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--mapper resource="mapper/userMapper.xml"/-->
<mapper resource="mapper/userMapper01.xml"/>
</mappers>
</configuration>
userMapper01.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.hetl.day02.dao.UserMapper">
<insert id="save" parameterType="com.hetl.day01.pojo.User">
insert into user (username,sex,birthday,address)
value (#{username},#{sex},#{birthday},#{address})
</insert>
<select id="findUserByName" parameterType="String" resultType="user">
SELECT * from user where username like '%${value}%'
</select>
</mapper>
UserMapper.java
public interface UserMapper {
public int save(User user);
public List<User> findUserByName(String s);
}
测试
public class Test02 {
SqlSessionFactory ssf;
SqlSession ss;
@Before
public void before() throws IOException {
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
ssf = new SqlSessionFactoryBuilder().build(in);
ss = ssf.openSession();
}
@Test
public void test1(){
UserMapper u = ss.getMapper(UserMapper.class);
List<User> list = u.findUserByName("he");
System.out.println(list);
ss.commit();
ss.close();
}
}
Mybatis的映射文件
输入映射 parameterType
传入简单类型如int String等
传入pojo对象
传递pojo包装对象
传递map对象
输出映射resultType / resultMap
输出类型int String等
输出类型 、pojo对象
输出类型 pojo包装类
<resultMap id="userResultMap" type="user">
<id column="id_" property="id"/>
<result column="username_" property="username"/>
<result column="sex_" property="sex"/>
<result column="birthday_" property="birthday"/>
<result column="address_" property="address"/>
</resultMap>
<select id="findUserByIdResultMap" parameterType="int" resultMap="userResultMap">
select id id_,username username_,sex sex_,birthday birthday_,address address_
from user
where id = #{?};
</select>
public User findUserByIdResultMap(int id);