Mybatis基本配置和搭建
2015-10-28 21:22
302 查看
今天,主要向大家分享下如何从头搭建一个最简单的mybatis项目
接下来为大家解释下该配置文件中的内容
1. settings
定义mybatis的一些全局设置,这里 配置的两个属性
mapUnderscoreToCamelCase: 在映射database column名字和entity属性名时,自动将带下划线column名转化为常见的java驼峰形式属性名
lazyLoadingEnabled: 延迟加载entity的关联属性
2. typeAlias
定义java类型的别名,比如这里分别将com.yun.entity.Employee和com.yun.entity.Customer设置别名为Employee,Customer。这样在别处配置文件中使用 它们时,就不必再指明带package全名。
3. environments
主要用于配置数据源
可以配置多个environment,以用于不同的产品环境,这里只配置一个用于测试,并定义id为“local”
transactionManager: 有两种类型
1, JDBC : 使用从数据源返回的连接管理commit和rollback
2, MANAGED : 依靠容器来管理transaction
dataSource: 有3种类型
1, UNPOOLED :每次请求新打开连接,用完后关闭连接
2, POOLED : 使用连接池管理连接
3, JNDI :使用容器管理datasource时使用
4. mappers
简而言之,mapper文件用于定义sql语句,以及与其对应的entity
以employee-mapper.xml为例,以下是其简单实现:
这里,我们定义了2句sql statement,分别是
selectAllEmployee: 查找所有employee
selectEmployeeById:根据id查找特定employee
返回结果类型均为Employee(看!这里typeAlias就起到作用了,不需要指明class全名),区别在于前者是个List。
这里大家可能会对selectEmployeeById 中的#{id}感到疑惑,别急,下面会为您介绍。
NOTE:配置文件中的顺序不能乱来,对于例子中的几个配置,要按照顺序来定义:settings -> typeAliases -> environments -> mappers.因为mybatis是按照一个固定顺序来解析这个配置文件的,如果顺序不对,加载时会报错。
通过调用MySqlSession.newSqlSession()即可获得一个SQLSession对象,这也是mybatis中最核心的类了,负责各种select,update,delete接口等,这里就不详细解释了。
还是直接上代码~
看到这里,想必各位看官也知道前面#{id}的value是哪里来的了,是的,它就是 getEmployeeById(Long id)中的参数,但两者名字不要求一致哦。
好了,到这里,一个最简单的mybatis配置和实现已经结束了,希望对您有所帮助。
更多细节参看mybatis官方文档:http://mybatis.github.io/mybatis-3/
下载地址
Mybatis3 最新下载地址:https://github.com/mybatis/mybatis-3/releases一, 创建配置文件
在新建的project的src目录下,新建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> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> <setting name="lazyLoadingEnabled" value="true"/> </settings> <typeAliases> <typeAlias type="com.test.entity.Employee" alias="Employee"/> <typeAlias type="com.test.entity.Customer" alias="Customer"/> </typeAliases> <environments default="local"> <environment id="local"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/orcl"/> <property name="username" value="username"/> <property name="password" value="password"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/test/data/employee-mapper.xml"/> <mapper resource="com/test/data/customer-mapper.xml"/> </mappers> </configuration>
接下来为大家解释下该配置文件中的内容
1. settings
定义mybatis的一些全局设置,这里 配置的两个属性
mapUnderscoreToCamelCase: 在映射database column名字和entity属性名时,自动将带下划线column名转化为常见的java驼峰形式属性名
lazyLoadingEnabled: 延迟加载entity的关联属性
2. typeAlias
定义java类型的别名,比如这里分别将com.yun.entity.Employee和com.yun.entity.Customer设置别名为Employee,Customer。这样在别处配置文件中使用 它们时,就不必再指明带package全名。
3. environments
主要用于配置数据源
可以配置多个environment,以用于不同的产品环境,这里只配置一个用于测试,并定义id为“local”
transactionManager: 有两种类型
1, JDBC : 使用从数据源返回的连接管理commit和rollback
2, MANAGED : 依靠容器来管理transaction
dataSource: 有3种类型
1, UNPOOLED :每次请求新打开连接,用完后关闭连接
2, POOLED : 使用连接池管理连接
3, JNDI :使用容器管理datasource时使用
4. mappers
简而言之,mapper文件用于定义sql语句,以及与其对应的entity
以employee-mapper.xml为例,以下是其简单实现:
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.test.data.EmployeeMapper"> <select id="selectAllEmployee" resultType="Employee"> select * from employee </select> <select id="selectEmployeeById" parameterType="long" resultType="Employee"> select * from employee where employee_id=#{id} </select> </mapper>
这里,我们定义了2句sql statement,分别是
selectAllEmployee: 查找所有employee
selectEmployeeById:根据id查找特定employee
返回结果类型均为Employee(看!这里typeAlias就起到作用了,不需要指明class全名),区别在于前者是个List。
这里大家可能会对selectEmployeeById 中的#{id}感到疑惑,别急,下面会为您介绍。
NOTE:配置文件中的顺序不能乱来,对于例子中的几个配置,要按照顺序来定义:settings -> typeAliases -> environments -> mappers.因为mybatis是按照一个固定顺序来解析这个配置文件的,如果顺序不对,加载时会报错。
二,获得SqlSession
不赘述,直接上代码public class MySqlSession { private static String mybatisResource = "mybatis-config.xml"; private static SqlSessionFactory sqlSessionFactory; public static SqlSessionFactory getSqlSessionFactory() { try { if (sqlSessionFactory == null) { InputStream inputStream = Resources.getResourceAsStream(mybatisResource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } return sqlSessionFactory; } catch (IOException e) { throw new RuntimeException(e); } } public static SqlSession newSqlSession() { SqlSession session = getSqlSessionFactory().openSession(); return session; } }
通过调用MySqlSession.newSqlSession()即可获得一个SQLSession对象,这也是mybatis中最核心的类了,负责各种select,update,delete接口等,这里就不详细解释了。
三,DAO
马上就到最后一步啦!还是直接上代码~
public class BatisEmployeeDao { public List<Employee> getAllEmployees() { SqlSession session = MySqlSession.newSqlSession(); try { List<Employee> ret = session.selectList("selectAllEmployee"); return ret; } finally { session.close(); } } public Employee getEmployeeById(Long id) { SqlSession session = MySqlSession.newSqlSession(); try { Employee employee = session.selectOne("selectEmployeeById", id); return employee; } finally { session.close(); } } }
看到这里,想必各位看官也知道前面#{id}的value是哪里来的了,是的,它就是 getEmployeeById(Long id)中的参数,但两者名字不要求一致哦。
好了,到这里,一个最简单的mybatis配置和实现已经结束了,希望对您有所帮助。
更多细节参看mybatis官方文档:http://mybatis.github.io/mybatis-3/
相关文章推荐
- 《需求工程-软件建模与分析》读书笔记3
- 字体图标 iconfont cssfont
- 归算法及经典递归例子代码实现
- NOI2015酱油记
- 一些关于style和attr的使用问题
- 二线蓝筹股
- HDOJ 1166 敌兵布阵 (树状数组 & 线段树)
- Oracle函数之非常实用的统计查询
- Hadoop优化点记录
- OC学习_ 第三天 继承、初始化方法、便利构造器
- LintCode_x的平方根
- 类型检查(一)
- Java:IO流其他类(字节数组流、字符数组流、数据流、打印流、Properities、对象流、管道流、随机访问、序列流、字符串读写流)
- swift 的循环
- Android项目-Activity跳转动画、状态选择器、Shape
- Xray6
- JAVA Map集合框架的使用
- JQ数组=jQuery对象数组
- 为什么需要大数据安全分析?
- 05-树7 堆中的路径