mybatis入门

在classpath下创建SqlMapConfig.xml,如下:

 1 <!DOCTYPE configuration
 2 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 3 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 4 <configuration>
 5     <!-- 和spring整合后 environments配置将废除-->
 6     <environments default="development">
 7         <environment id="development">
 8         <!-- 使用jdbc事务管理-->
 9             <transactionManager type="JDBC" />
10         <!-- 数据库连接池-->
11             <dataSource type="POOLED">
12                 <property name="driver" value="com.mysql.jdbc.Driver" />
13                 <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" />
14                 <property name="username" value="root" />
15                 <property name="password" value="root" />
16             </dataSource>
17         </environment>
18     </environments>

SqlMapConfig.xml是mybatis核心配置文件,上边文件的配置内容为数据源、事务管理。

 

Po类作为mybatis进行sql映射使用,po类通常与数据库表对应,User.java如下:

1 Public class User {
2     private int id;
3     private String username;// 用户姓名
4     private String sex;// 性别
5     private Date birthday;// 生日
6     private String address;// 地址

映射文件:

在classpath下的sqlmap目录下创建sql映射文件User.xml:

 1 <!DOCTYPE mapper
 2 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 4 
 5 <!-- 命名空间(sql隔离) -->
 6 <mapper namespace="test">
 7     <!-- 通过select执行数据库查询  #{}表示占位符 id接受输入的参数
 8     resulttype:输出结果所对应的类型
 9     -->
10     <select id="findUserById" parameterType="int" resultType="cn.cuibusimybatis.po.User">
11         select * from user where id=#{id}
12     </select>
13 </mapper>

parameterType:定义输入到sql中的映射类型,#{id}表示使用preparedstatement设置占位符号并将输入变量id传到sql。

resultType:定义结果映射类型。

 

在SqlMapConfig.xml中添加:

1 <!-- 加载映射文件 -->
2     <mappers>
3         <mapper resource="sqlmap/User.xml"/>
4     </mappers>

 

测试程序:

 1 //根据id查询用户信息
 2     @Test
 3     public void findUserById() throws IOException {
 4         // mybatis配置文件
 5         String resource = "SqlMapConfig.xml";
 6         // 得到配置文件流
 7         InputStream inputStream = Resources.getResourceAsStream(resource);
 8         // 创建会话工厂,传入mybatis的配置文件信息
 9         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
10         // 通过工厂得到SqlSession
11         SqlSession sqlSession = sqlSessionFactory.openSession();
12         // 通过SqlSession操作数据库
13         // 第一个参数:映射文件中statement的id,等于=namespace+"."+statement的id
14         // 第二个参数:指定和映射文件中所匹配的parameterType类型的参数
15         // sqlSession.selectOne结果 是与映射文件中所匹配的resultType类型的对象
16         // selectOne查询出一条记录
17         User user = sqlSession.selectOne("test.findUserById", 1);
18         System.out.println(user);
19         sqlSession.close();
20     }

结果:

 

转载于:https://www.cnblogs.com/cuibin/p/6759007.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值