Mybatis基本配置和搭建
来源:互联网 发布:韩风男装淘宝店 编辑:程序博客网 时间:2024/05/22 16:02
今天,主要向大家分享下如何从头搭建一个最简单的mybatis项目
下载地址
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 : 依靠容器来管理transactiondataSource: 有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/
- Mybatis基本配置和搭建
- MyBatis:配置和搭建
- 【mybatis】基本配置和使用
- SpringMVC和MyBatis基本框架搭建
- MyBatis入门-基本配置和查询
- Mybatis的基本配置和使用
- MyBatis 框架的搭建和配置
- mybatis基本环境搭建
- mybatis基本环境搭建
- Mybatis基本环境搭建
- mybatis-mybatis的基本配置
- (一)mybatis 环境搭建和基本说明
- MyBatis框架的搭建和基本的写法事列
- Nginx服务器搭建和基本配置详解
- mybatis 基本配置
- MyBatis-genenator基本配置
- MyBatis的基本配置
- Mybatis-spring基本配置
- Hadoop优化点记录
- 归算法及经典递归例子代码实现
- LintCode_不同的路径
- Oracle函数之非常实用的统计查询
- 自定义View实现支付宝芝麻信用页面功能
- Mybatis基本配置和搭建
- 蓝懿iOS 技术内容和心得 10。28技术内容交流和心得分享
- 2015-10-28
- 显式调用构造函数和析构函数
- Hadoop2.x下安装HBase
- 私有构造函数
- 用ajax判断帐号是否存在(附例子)
- SpringMVC-初识SpringMVC
- 动态规划