mybatis学习笔记(二)初级使用mybatis
mybatis简介
mybatis是一款基于ORM的半自动轻量级持久层框架。ROM表示对象-关系映射的缩写。半自动体现在自定义sql语句。轻量级是指其运行时耗费的资源较少。
mybatis基础应用
1、引入mybatis依赖
<dependencies>
<!--mybatis坐标-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--mysql驱动坐标-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
<scope>runtime</scope>
</dependency>
<!--单元测试坐标-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--日志坐标-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
</dependencies>
2、创建User库表以及User实体
此处不列出,其中属性为username,id
3、编写UserMapper映射文件
<?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.lagou.dao.IUserDao">
<select id="findAll" parameterType="User" resultType="User">
SELECT * FROM user;
</select>
<delete id="deleteOne" parameterType="int">
DELETE FROM USER WHERE id=#{aaa}
</delete>
<update id="updateOne" parameterType="User">
UPDATE USER SET username=#{username} WHERE id=#{id}
</update>
<insert id="addOne" parameterType="User">
INSERT INTO USER(id,username) VALUES (#{id},#{username})
</insert>
<select id="findByIds" parameterType="list" resultType="User">
SELECT * FROM USER
<where>
<foreach collection="array" open="id in (" close=")" item="id" separator=",">
#{id}
</foreach>
</where>
</select>
</mapper>
4、编写MyBatis核心文件
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties"></properties>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<package name="com.lagou.pojo"></package>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="UserMapper.xml"></mapper>
</mappers>
</configuration>
5、编写jdbc.properties配置文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///test
jdbc.username=root
jdbc.password=root
6、编写测试代码
@Test
public void test() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = build.openSession();
List<User> list = sqlSession.selectList("user.selectList");
list.forEach(System.out::println);
sqlSession.clearCache();
}
7、编写UserDao接口以及其实现(传统方法)
public interface IUserDao {
List<User> findAll() throws IOException;
void addOne(User user);
List<User> findByIds(int[] arr);
}
public class UserDaoImpl implements UserDao {
public List<User> findAll() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
List<User> userList = sqlSession.selectList("userMapper.findAll"); sqlSession.close();
return userList;
}
}
测试方法
@Test public void testTraditionDao() throws IOException {
UserDao userDao = new UserDaoImpl();
List<User> all = userDao.findAll();
System.out.println(all);
}
8、代理userDao方法开发
代理开发模式只需要创建对应userDao即可,不需要去userDao的实现类,底层通过jdk的动态代理来生成代理类userDao,调用userDao中的方法时,执行invoke方法中的逻辑来实现(后期学习笔记中会详细讲到)
其中代理开发需要遵从的规则:
1、Mapper.xml文件中的namespace与mapper接口的全限定名相同
2、Mapper接口方法名和Mapper.xml中定义的每个statement的id相同
3、Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql的parameterType的类型相同
4、Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同