黑马SSM教程MyBatis框架学习笔记
1. 原始JDBC操作的缺点及MyBatis的解决方案
2. MyBatis快速入门
public class Test1 {
@Test
public void test1() throws IOException {
//获得核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
//获得SQLSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得session会话对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行操作 参数:namespace + id
List<Student> stuList = sqlSession.selectList("studentMapper.findAll");
//测试数据
System.out.println(stuList);
//释放资源
sqlSession.commit();
sqlSession.close();
}
}
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>
<!--数据源环境-->
<environments default="server-MySql">
<environment id="server-MySql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://120.26.1.6/demo"/>
<property name="username" value="root"/>
<property name="password" value="$$$$$$$$$$$"/>
</dataSource>
</environment>
<environment id="localServer">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/demo"/>
<property name="username" value="root"/>
<property name="password" value="$$$$$$$"/>
</dataSource>
</environment>
</environments>
<!--加载映射文件-->
<mappers>
<mapper resource="com/itheima/mapper/StudentMapper.xml"/>
</mappers>
</configuration>
StudentMapper.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="studentMapper">
<select id="findAll" resultType="com.itheima.domain.Student">
select * from stu
</select>
</mapper>
3. MyBatis增删改查
插入:
#{实体属性名}
<insert id="insert" parameterType="com.itheima.domain.Student">
insert into stu values (#{id}, #{name})
</insert>
sqlSession.insert("studentMapper.insert", student);
其他数据库操作:
4.MyBatis核心配置文件
- <envoroments><envoroments\>标签
略 - <mappers><mappers\>
- <properties><properties\>
加载properties文件,可以使用EL表达式读取&{}
<properties resources="jdbc.properties"></properties>
- <typeAliases><typeAliases\>
自定义别名
除了可以设置自定义的别名,mybatis已经为我们设置好了很多常见的别名,如int,long,double,string…
- typeHandlers标签
5. MyBatis的Dao层实现(重要)
代理开发方式规则
public class ServiceDemo {
public static void main(String[] args) throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = build.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> all = mapper.findAll();
System.out.println(all);
}
}
6.动态SQL
Mybatis的自动映射和驼峰映射
当我们数据库中字段名为roleName且对应的pojo中的属性名称也为roleName时,可以完成自动映射。
是否开启驼峰命名自动映射,即从经典数据库列名 A_COLUMN 映射到经典 Java 属性名 aColumn。默认关闭。
- if
xml使用示例
<select id="selectByCondition" resultType="com.itheima.domian.User">
select * from user
<where>
<if test="id!=0">
id_ = #{id}
</if>
<if test="username!=null">
and username_ = #{username}
</if>
<if test="password!=null">
and password_ = #{password}
</if>
</where>
</select>
- choose(when, otherwise)
List<User> selectByIds(List<Integer> ids);
根据ids中的值进行查询
list = [1, 2, 3] 对应的SQL语句为:select * from user where id in (1,2,3)
怎么进行动态查询呢?
<select id="selectByIds" parameterType="list" resultType="com.itheima.domian.User">
select * from user
<where>
<foreach collection="list" item="id" open="id_ in(" close=")" separator=",">
#{id}
</foreach>
</where>
</select>
分析:
SQL语句应该为select * from user where id_ in (1,2,3)
其中select * from user为固定的,直接书写。
根据集合中是否有元素来决定是否有where子句,所以使用<where>标签
where子句中 open 和 close 分别为变化部分的左右不变部分
最后用foreach取出集合中的值,#{itemValue}
- trim(where, set)
- foreach