1、mybatis
mybatis是一个自定义sql、存储过程和高级映射的持久层框架,是Apache下的顶级项目。
mybatis可以让程序员将主要精力放在sql上,通过mybatis提供的映射方式。自由灵活生成满足需要的sql。
mybatis可以将向prepareStatement中的输入参数自动进行输入映射,将查询结果集自动映射成Java对象。
2、mybatis框架
3、最主要的配置文件SqlMapConfig.xml
配置数据库连接等。
映射文件
映射文件命名方式(来自ibatis命名),mapper代理开发映射文件名称叫xxxMapper.xml,在映射文件中配置sql。
4、实例演示
功能:MySQL中有一个user表,通过ID来查询user和通过name来模糊查询user。
(1)配置SqlMapConfig.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" > < configuration > <!--和spring整合后environment配置将废除 --> < environments default = "development" > < environment id = development > <!-- 使用jdbc事务管理,事务控制由mybatis --> < transactionManager type = "JDBC" ></ transactionManager > <!-- 数据库连接池,由mybatis管理 --> < dataSource type = "POOLED" > < property name = "driver" value = "com.mysql.jdbc.Driver" /> < property name = "url" value = "jdbc:mysql://127.0.0.1:3306/databasename" /> < property name = "username" value = "root" /> < property name = "password" value = "mysql" /> </ dataSource > </ environment > </ environments > <!-- 加载映射文件 --> < mappers > < mapper resource = "resources/mapper/UserMapper.xml" /> </ mappers > </ configuration > |
(2)配置UserMapper.xml。各种sql的使用都在这里来映射。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<? 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" > <!--namespace命名空间,作用就是对sql进行分类化管理,理解sql隔离 不同表可以施工不同的命名空间 注意:使用mapper代理方法开发,namespace就有特殊重要的作用 --> < mapper namespace = "test" > <!--在映射文件中配置很多sql语句 --> <!--通过select执行数据库查询 ID:标识映射文件中的sql,将sql语句封装到mapperStatement对象中,所以id就是statement的id parameterType:指定输入参数类型,这个根据数据库字段类型来的 #():标识一个占位符 #(id):其中id表示接收输入参数。参数名称就是id,如果输入参数是简单类型,#()中的参数名词可以任意,可以是value或者其他 resultType:指定sql输出结果集所映射的Java对象。 --> < select id = "findById" parameterType = "int" resultType = "com.hust.wt.model.User" > select * from user where id=#(); </ select > <!-- resultType指定的就是单条记录所映射的Java对象类型 ${}:表示拼接sql串,将接收到参数的内容不叫任何修饰【拼接在sql中。使用${}会引起sql注入 ${value}:接收输入参数的内容,如果传入类型是简单类型,${}中只能是value --> < select id = "findByName" parameterType = "java.lang.String" resultType = "com.hust.wt.model.User" > select * from user where name like ‘%${value}%’; </ select > </ mapper > |
(3)测试程序:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public class TestOne { @Test public void findByIdTest() throws IOException{ //mybatis的配置文件 String resource = "conf/SqlMapConfig.xml" ; //得到配置文件流 InputStream inputStream = Resources.getResourceAsStream(resource); //创建会话工厂 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //通过工厂得到会话 SqlSession sqlSession = sqlSessionFactory.openSession(); //通过SQlSession来操作数据库 //第一个参数statement:映射文件statement的id,等于=命名空间+‘。’+statement的id, //第二个参数Parameter指定和映射文件中所匹配的ParameterType类型的参数 //返回的结果就是映射文件中的resultType类型的对象 User user=sqlSession.selectOne( "test.findById" , 1 ); System.out.println(user); //释放资源 sqlSession.close(); } } |
整个执行过程就可以使用debug来走一遍。
对于第二个select,就是另一个不同的需求,可以在写一个test方法来测试,这里忽略。
(4)小结
parameterType:输入参数类型
resultType:指定输出结果的类型
#{}:#{}代表占位符,类型可以是简单类型,model(pojo类)、HashMap。使用其接受pojo对象值是通过OGNL读取对象中的属性值。如果pojo中还有pojo类,那么就是user.user.username来读取属性值,即属性.属性。
?${}是拼接符号,拼接sql语句,但是这会引起sql注入
selectOne表示查询出一条记录进行映射,如果使用selectOne那么也可以使用selectList(列表记录为1条)
selectList表示查询出一个列表(多条记录)进行映射。但不能使用selectOne来替代。
(5)扩展功能
添加用户:在UserMapper.xml中添加insert
1
2
3
4
|
<!-- 添加用户 parameterType为model类型是复杂对象 --> < insert id = "insertUser" parameterType = "com.hust.wt.model.User" > insert into user(id,name,password,age) value(#{id},#{name},#{password},#{age}) </ insert > |
在测试程序中需要 提交事务sqlSession.commit();
删除用户和更新用户:在UserMapper.xml中添加
1
2
3
4
5
6
7
|
< delete id = "deleteUser" parameterType = "java.lang.Integer" > delete from user where id=#{id} </ delete > < update id = "updateUser" parameterType = "com.hust.wt.model.User" > update user set name=#{name}, password=#{password},age=#{age},where id=#{id} </ update > |
(6)hibernate和mybatis的区别
hibernate:是一个标准ORM框架(对象关系映射)入门门槛较高,不需要写sql语句,自动生成sql语句。对sql的优化和修改会比较困难
使用场景:适用需求变化不多的中小型项目,比如ERP,ORM,OA等。
mybatis:专注的是sql本身,需要程序员自己去编写sql语句,便于语句的优化和修改。mybatis可以裂解成一个不完全的ORM框架,虽然程序员可以自己写sql,也可以实现映射(输入输出)。
使用场景:适用于需求变化较多的项目,比如互联网项目。
原文:http://www.cnblogs.com/silence-hust/p/5071556.html