标签:mybatis mybatis快速入门 mybatis简单实例
【总结内容来自 尚硅谷 资料】
发博备查。
1. mybatis 快速入门:
1). 添加 jar 包
mybatis-3.1.1.jar
mysql-connector-java-5.1.7-bin.jar
2). 建库建表
create database mybatis; use mybatis; CREATE TABLE users(id INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(20), age INT); INSERT INTO users(NAME, age) VALUES(‘Tom‘, 12); INSERT INTO users(NAME, age) VALUES(‘Jack‘, 11);
3). 添加 Mybatis 的配置文件 conf.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="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatis" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> </configuration>
4). 定义表所对应的实体类
public class User { private int id; private String name; private int age; //get,set 方法 }
5). 定义操作 users 表的 sql 映射文件 userMapper.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.irvin.mybatis_test.test1.userMapper"> <select id="getUser" parameterType="int" resultType="com.irvin.mybatis_test.test1.User"> select * from users where id=#{id} </select> </mapper>
6). 在 conf.xml 文件中注册 userMapper.xml 文件
<mappers> <mapper resource="com/irvin/mybatis_test/test1/userMapper.xml"/> </mappers>
7). 编写测试代码:执行定义的 select 语句
public class Test { public static void main(String[] args) throws IOException { String resource = "conf.xml"; //加载 mybatis 的配置文件(它也加载关联的映射文件) Reader reader = Resources.getResourceAsReader(resource); //构建 sqlSession 的工厂 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); //创建能执行映射文件中 sql 的 sqlSession SqlSession session = sessionFactory.openSession(); //映射 sql 的标识字符串 String statement = "com.atguigu.mybatis.bean.userMapper"+".selectUser"; //执行查询返回一个唯一user 对象的sql User user = session.selectOne(statement, 1); System.out.println(user); } }
2. 一对一关联:
<!--
方式一:嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集
封装联表查询的数据(去除重复的数据)
select * from class c, teacher t where c.teacher_id=t.t_id and c.c_id=1
-->
<select id="getClass" parameterType="int" resultMap="ClassResultMap">
select * from class c, teacher t where c.teacher_id=t.t_id and c.c_id=#{id}
</select>
<resultMap type="_Classes" id="ClassResultMap">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" column="teacher_id" javaType="_Teacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
</association>
</resultMap>
<!--
方式二:嵌套查询:通过执行另外一个SQL 映射语句来返回预期的复杂类型
SELECT * FROM class WHERE c_id=1;
SELECT * FROM teacher WHERE t_id=1 //1 是上一个查询得到的teacher_id 的值
-->
<select id="getClass2" parameterType="int" resultMap="ClassResultMap2">
select * from class where c_id=#{id}
</select>
<resultMap type="_Classes" id="ClassResultMap2">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" column="teacher_id" javaType="_Teacher"
select="getTeacher">
</association>
</resultMap>
<select id="getTeacher" parameterType="int" resultType="_Teacher">
SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id}
</select>
<!--
association 用于一对一关联查询
property : 对象属性的名称
javaType ;对象属性的类型
column : 所对应的外键字段名称
select : 使用另一个查询封装的结果
-->
3. 一对多关联:
<!--
方式一: 嵌套结果: 使用嵌套结果映射来处理重复的联合结果的子集
SELECT * FROM class c, teacher t,student s WHERE c.teacher_id=t.t_id AND c.C_id=s.class_id AND c.c_id=1
-->
<select id="getClass3" parameterType="int" resultMap="ClassResultMap3">
select * from class c, teacher t,student s where c.teacher_id=t.t_id and c.C_id=s.class_id and c.c_id=#{id}
</select>
<resultMap type="_Classes" id="ClassResultMap3">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" column="teacher_id" javaType="_Teacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
</association>
<!-- ofType 指定students 集合中的对象类型 -->
<collection property="students" ofType="_Student">
<id property="id" column="s_id"/>
<result property="name" column="s_name"/>
</collection>
</resultMap>
<!--
方式二:嵌套查询:通过执行另外一个SQL 映射语句来返回预期的复杂类型
SELECT * FROM class WHERE c_id=1;
SELECT * FROM teacher WHERE t_id=1 //1 是上一个查询得到的teacher_id 的值
SELECT * FROM student WHERE class_id=1 //1 是第一个查询得到的c_id 字段的值
-->
<select id="getClass4" parameterType="int" resultMap="ClassResultMap4">
select * from class where c_id=#{id}
</select>
<resultMap type="_Classes" id="ClassResultMap4">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" column="teacher_id" javaType="_Teacher"
select="getTeacher2"></association>
<collection property="students" ofType="_Student" column="c_id" select="getStudent"></collection>
</resultMap>
<select id="getTeacher2" parameterType="int" resultType="_Teacher">
SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id}
</select>
<select id="getStudent" parameterType="int" resultType="_Student">
SELECT s_id id, s_name name FROM student WHERE class_id=#{id}
</select>
<!--
collection : 做一对多关联查询
ofType : 指定集合中元素对象的类型
-->
4. 动态 SQL 与模糊查询
<?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.irvin.day03_mybatis.test6.userMapper">
<select id="getUser" parameterType="com.irvin.day03_mybatis.test6.ConditionUser"
resultType="com.irvin.day03_mybatis.test6.User">
select * from d_user where age>=#{minAge} and age<=#{maxAge}
<if test=‘name!="%null%"‘>and name like #{name}</if>
</select>
</mapper>
<!--
MyBatis 中可用的动态SQL 标签:
if
choose(when, otherwise)
trim(where, set)
foreach
-->
5. 调用存储过程
<mapper namespace="com.atguigu.day03_mybatis.test7.userMapper">
<select id="getCount" resultType="java.util.Map" statementType="CALLABLE">
{call
ges_user_count(#{sex_id,mode=IN,jdbcType=INTEGER},#{result,mode=OUT,jdbcType=INTEGER})
}
</select>
</mapper>
<!--
<select>
parameterMap : 引用 <parameterMap>
statementType : 指定 statement 的真是类型 : CALLABLE 执行调用存储过程的语句
<parameterMap> : 定义多个参数的键值对
type : 需要传递的参数的真实类型 java.util.Map
<parameter> : 指定一个参数 key - value
-->
6. 缓存机制 :
1). 一级缓存:
基于PerpetualCache 的 HashMap 本地缓存,其存储作用域为 Session,当 Session flush 或 close 之后,该Session 中的所有 Cache 就将清空。
/*
* 一级缓存: 也就Session 级的缓存(默认开启)
*/
@Test
public void testCache1() {
SqlSession session = MybatisUtils.getSession();
String statement = "com.atguigu.mybatis.test8.userMapper.getUser";
User user = session.selectOne(statement, 1);
System.out.println(user);
/*
* 一级缓存默认就会被使用
*/
/*
user = session.selectOne(statement, 1);
System.out.println(user);
*/
/*
1. 必须是同一个Session,如果session 对象已经close()过了就不可能用了
*/
/*
session = MybatisUtils.getSession();
user = session.selectOne(statement, 1);
System.out.println(user);
*/
/*
2. 查询条件是一样的
*/
/*
user = session.selectOne(statement, 2);
System.out.println(user);
*/
/*
3. 没有执行过session.clearCache()清理缓存
*/
/*
session.clearCache();
user = session.selectOne(statement, 2);
System.out.println(user);
*/
/*
4. 没有执行过增删改的操作(这些操作都会清理缓存)
*/
/*
session.update("com.atguigu.mybatis.test8.userMapper.updateUser",
new User(2, "user", 23));
user = session.selectOne(statement, 2);
System.out.println(user);
*/
}
2). 二级缓存与一级缓存其机制相同,默认也是采用 PerpetualCache,HashMap 存储,不同在于其存储作用域为 Mapper(Namespace),并且可自定义存储源,如 Ehcache。
3). 对于缓存数据更新机制,当某一个作用域(一级缓存Session/二级缓存Namespaces)的进行了C/U/D 操作后,默认该作用域下所有 select 中的缓存将被clear。
添加一个<cache>在userMapper.xml 中
<mapper namespace="com.atguigu.mybatis.test8.userMapper">
<cache/>
<!--
补充说明:
1. 映射语句文件中的所有select 语句将会被缓存。
2. 映射语句文件中的所有insert,update 和delete 语句会刷新缓存。
3. 缓存会使用Least Recently Used(LRU,最近最少使用的)算法来收回。
4. 缓存会根据指定的时间间隔来刷新。
5. 缓存会存储1024 个对象
-->
<!--
<cache
eviction="FIFO" //回收策略为先进先出
flushInterval="60000" //自动刷新时间60s
size="512" //最多缓存512 个引用对象
readOnly="true"/> //只读
-->
7. spring 集成 mybatis
1). 添加Jar 包
[mybatis]
mybatis-3.2.0.jar
mybatis-spring-1.1.1.jar
log4j-1.2.17.jar
[spring]
spring-aop-3.2.0.RELEASE.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
spring-jdbc-3.2.0.RELEASE.jar
spring-test-3.2.4.RELEASE.jar
spring-tx-3.2.0.RELEASE.jar
aopalliance-1.0.jar
cglib-nodep-2.2.3.jar
commons-logging-1.1.1.jar
[MYSQL 驱动包]
mysql-connector-java-5.0.4-bin.jar
2). 数据库表
CREATE TABLE s_user(
user_id INT AUTO_INCREMENT PRIMARY KEY,
user_name VARCHAR(30),
user_birthday DATE,
user_salary DOUBLE
)
3). 实体类: User
public class User {
private int id;
private String name;
private Date birthday;
private double salary;
//set,get 方法
}
4). DAO 接口: UserMapper (XXXMapper)
public interface UserMapper {
void save(User user);
void update(User user);
void delete(int id);
User findById(int id);
List<User> findAll();
}
5). SQL 映射文件: userMapper.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.atguigu.mybatis.test9.UserMapper">
<resultMap type="User" id="userResult">
<result column="user_id" property="id"/>
<result column="user_name" property="name"/>
<result column="user_birthday" property="birthday"/>
<result column="user_salary" property="salary"/>
</resultMap>
<!-- 取得插入数据后的id -->
<insert id="save" keyColumn="user_id" keyProperty="id" useGeneratedKeys="true">
insert into s_user(user_name,user_birthday,user_salary)
values(#{name},#{birthday},#{salary})
</insert>
<update id="update">
update s_user
set user_name = #{name},
user_birthday = #{birthday},
user_salary = #{salary}
where user_id = #{id}
</update>
<delete id="delete">
delete from s_user
where user_id = #{id}
</delete>
<select id="findById" resultMap="userResult">
select * from s_user
where user_id = #{id}
</select>
<select id="findAll" resultMap="userResult">
select *
from s_user
</select>
</mapper>
6). spring 的配置文件: beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 1. 数据源 : DriverManagerDataSource -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- 2. mybatis 的SqlSession 的工厂: SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.irvin.spring_mybatis2.domain"/>
</bean>
<!-- 3. mybatis 自动扫描加载Sql 映射文件 : MapperScannerConfigurer -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.irvin.spring_mybatis2.mapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!-- 4. 事务管理 : DataSourceTransactionManager -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 5. 使用声明式事务 -->
<tx:annotation-driven transaction-manager="txManager" />
</beans>
7). mybatis 的配置文件: 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>
<!-- Spring 整合myBatis 后,这个配置文件基本可以不要了-->
<!-- 设置外部配置文件 -->
<!-- 设置类别名 -->
<!-- 设置数据库连接环境 -->
<!-- 映射文件 -->
</configuration>
8). 测试
@RunWith(SpringJUnit4ClassRunner.class) //使用Springtest框架
@ContextConfiguration("/beans.xml") //加载配置
public class SMTest {
@Autowired //注入
private UserMapper userMapper;
@Test
public void save() {
User user = new User();
user.setBirthday(new Date());
user.setName("marry");
user.setSalary(300);
userMapper.save(user);
System.out.println(user.getId());
}
@Test
public void update() {
User user = userMapper.findById(2);
user.setSalary(2000);
userMapper.update(user);
}
@Test
public void delete() {
userMapper.delete(3);
}
@Test
public void findById() {
User user = userMapper.findById(1);
System.out.println(user);
}
@Test
public void findAll() {
List<User> users = userMapper.findAll();
System.out.println(users);
}
}
标签:mybatis mybatis快速入门 mybatis简单实例
原文地址:http://irvin.blog.51cto.com/9783109/1785629