mybatis基础使用

  MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 201311月迁移到Github

    MyBatis是一个优秀的持久层框架,它对jdbc的操作数据库的过程进行封装,使开发者只需要关注 SQL 本身,而不需要花费精力去处理例如注册驱动、创建connection、创建statement、手动设置参数、结果集检索等jdbc繁杂的过程代码。

  Mybatis通过xml或注解的方式将要执行的各种statementstatementpreparedStatemntCallableStatement)配置起来,并通过java对象和statement中的sql进行映射生成最终执行的sql语句,最后由mybatis框架执行sql并将结果映射成java对象并返回

目录结构

一、导包

  导入mybatis包及其所依赖的lib包

二、根据表创建相应的实体类

三、创建mybatis的配置文件sqlMapConfig.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6     <!-- 和spring整合后 environments配置将废除 -->
 7     <environments default="development">
 8         <environment id="development">
 9             <!-- 使用jdbc事务管理 -->
10             <transactionManager type="JDBC" />
11             <!-- 数据库连接池 -->
12             <dataSource type="POOLED">
13                 <property name="driver" value="com.mysql.jdbc.Driver" />
14                 <property name="url"
15                     value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" />
16                 <property name="username" value="root" />
17                 <property name="password" value="root" />
18             </dataSource>
19         </environment>
20     </environments>
21     
22     <!-- 加载映射文件 -->
23     <mappers>
24         <mapper resource="cn/tx/mapper/PersonTestMapper.xml"/>
25     </mappers>
26 </configuration>

二、配置映射文件PersonTestMapper.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <!-- namespace:命名空间,用于隔离sql,规范映射文件包的全路径 -->
 6 <mapper namespace="cn.tx.mapper.PersonTestMapper">
 7     <!-- resultMap用来解决属性与数据库表中列名不一致的情况 -->
 8     <resultMap type="cn.tx.model.Person" id="BaseResultMapper">
 9         <id column="id" property="id"/>
10         <result column="name" property="name"/>
11         <result column="gender" property="gender"/>
12         <result column="address" property="address"/>
13         <result column="birthday" property="birthday"/>
14     </resultMap>
15     <!-- 
16         id:sql语句唯一标识
17         parameterType:参数的类型
18         resultType:返回结果的类型
19      -->
20     <select id="selectPersonById" parameterType="java.lang.Integer" resultType="cn.tx.model.Person">
21         select * from person_test where id = #{id}
22     </select>
23     
24     <select id="selectPersonCount" resultType="java.lang.Integer">
25         select count(*) from person_test
26     </select>
27 </mapper>

三、运行测试

 1 public class TestMyBatis {
 2     private SqlSessionFactory sqlSessionFactory;
 3     @Before
 4     public  void setUp() throws Exception {
 5         //读取配置文件
 6         InputStream is = Resources.getResourceAsStream("sqlMapConfig.xml");
 7         //创建sqlSessionFactory
 8         sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
 9     }
10 
11     @Test
12     public void selectPersonById() {
13         //获得sqlSession
14         SqlSession sqlSession = sqlSessionFactory.openSession();
15         try {
16             Person person = sqlSession.selectOne("cn.tx.mapper.PersonTestMapper.selectPersonById", 1);
17             System.out.println(person);
18         } catch (Exception e) {
19             e.printStackTrace();
20         }finally {
21             sqlSession.close();
22         }
23     }
24     @Test
25     public void selectPersonCount(){
26         SqlSession sqlSession = sqlSessionFactory.openSession();
27         try {
28             Integer count = sqlSession.selectOne("cn.tx.mapper.PersonTestMapper.selectPersonCount");
29             System.out.println(count);
30         } catch (Exception e) {
31             e.printStackTrace();
32         }finally {
33             sqlSession.close();
34         }
35     }
36 
37 }

猜你喜欢

转载自www.cnblogs.com/cat-fish6/p/8933700.html
今日推荐