MyBatis入门教程
来源:互联网 发布:无网络也可以自动报时 编辑:程序博客网 时间:2024/06/05 19:10
MyBatis作为持久性框架,能够简化我们的编程。下面我们学一下如何利用MyBatis进行数据库连接
首先:应该导入myBatis的jar包mybatis-xxx和mysql驱动包mysql-connector-java-x.x.x-bin
1:
2创建与数据表对应的POJO Role ,字段名要与属性名一致。
package pojo;public class Role { private Long id; private String roleName; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } private String note;}
3:采用XML形式构建映射器,包含一个接口和一个XML
3.1 创建接口RoleMapper ,里面的方法是对Role对象进行操作
package mapper;import java.util.List;import pojo.Role;public interface RoleMapper { public int insertRole(Role role); public int deleteRole(Long id); public int updateRole(Role role); public Role getRole(Long id); public List<Role> findRoles(String roleName);}
3.2:创建XML文件,与接口形成映射
RoleMapper.xml
<?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"> <mapper namespace="mapper.RoleMapper"> <insert id="insertRole" parameterType="role"> insert into role(roleName,note) values(#{roleName},#{note}) </insert> <delete id="deleteRole" parameterType="long"> delete from role where id=#{id} </delete> <update id="updateRole" parameterType="role"> update role set roleName=#{roleName},note=#{note} where id=#{id} </update> <select id="getRole" parameterType="long" resultType="role"> select id,roleName,note from role where id=#{id} <!--id是方法中传递进来的参数--> </select> <select id="findRoles" parameterType="string" resultType="role"> select id,roleName,note from role where roleName=#{roleName} </select> </mapper>
4:配置mybatis-config.xml文件,用于创建SqlSessionFactory对象
<?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> <typeAliases > <!-- 别名方式1,一个一个的配置 type中放置的是类的全路径,alias中放置的是类别名 <typeAliase alias="role" type="pojo.Role"/> --> <!-- 别名方式2,自动扫描,将JAVA类的类名作为类的类别名 --> <package name="pojo"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <!-- 配置数据库连接信息 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/ssm"/> <property name="username" value="root" /> <property name="password" value="1367356" /> </dataSource> </environment> </environments> <mappers> <!-- 注册RoleMapper.xml文件, RoleMapper.xml位于mapper这个包下,所以resource写成mapper/RoleMapper.xml--> <mapper resource="mapper/RoleMapper.xml"/> </mappers> </configuration>
5使用SqlSessionFactoryBuilder来构建SqlSessionFactory
SqlSessionFactoryUtils.java
package utils;import java.io.InputStream;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class SqlSessionFactoryUtils { private final static Class<SqlSessionFactoryUtils> Lock = SqlSessionFactoryUtils.class; private static SqlSessionFactory SqlSessionFactory = null; // 构造函数 private SqlSessionFactoryUtils() { } public static SqlSessionFactory getSqlSessionFactory() { synchronized (Lock) { if (SqlSessionFactory != null) { return SqlSessionFactory; } String resource = "mybatis-config.xml"; InputStream inputStream; try { inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); return null; } return SqlSessionFactory; } } public static SqlSession openSqlSession() { if (SqlSessionFactory == null) { getSqlSessionFactory(); } return SqlSessionFactory.openSession(); }}
6创建测试类
Chapter3Main.java
package main;import org.apache.ibatis.session.SqlSession;import mapper.RoleMapper;import pojo.Role;import utils.SqlSessionFactoryUtils;public class Chapter3Main { public static void main(String[] args) { SqlSession sqlSession=null; try { sqlSession=SqlSessionFactoryUtils.openSqlSession(); RoleMapper roleMapper=sqlSession.getMapper(RoleMapper.class); Role role=roleMapper.getRole(1L); System.out.println(role.getRoleName()); } finally { // TODO: handle finally clause if(sqlSession!=null){ sqlSession.close(); } } }}
7:层次结构图
阅读全文
0 0
- mybatis入门教程
- mybatis入门教程
- Mybatis入门教程
- MyBatis入门教程
- mybatis入门教程
- mybatis入门教程
- mybatis入门教程
- mybatis入门教程
- MyBatis入门教程
- MyBatis 入门教程
- MyBatis入门教程
- mybatis入门教程
- mybatis入门教程(九)------mybatis缓存
- mybatis 简单入门教程
- mybatis入门教程 (一)
- Java Mybatis 框架入门教程
- MyBatis入门教程(一)
- mybatis一套完整入门教程
- 【NOIP2017模拟9.3A组】摘果子
- OA系统升级的经验分享,这是一个升华的过程,有痛苦有欢乐。文章转至麦枫网
- NP=P?
- HDU 4763 Theme Section【KMP的next数组练习】
- Linux IO中BIO和DIO
- MyBatis入门教程
- 怎么理解Condition
- UINavigationController
- 设计模式总结
- C语言 指针与整数相加/减,指针递增/递减示例
- 大O记号
- 深入理解C++中的异常处理机制
- 进程间通信(实例)复习(2)
- spring+hibernate+jpa+Druid的配置文件,spring整合Druid