赞
踩
mybatis参考网址:http://www.mybatis.org/mybatis-3/zh/index.html
Github源码地址:https://github.com/mybatis/mybatis-3
对象-关系映射(Object Relational Mapping,简称ORM),是随着面向对象的软件开发方法发展而产生的。把对象模型表示的对象映射到基于SQL 的关系模型数据库结构中去。在具体的操作实体对象的时候,就不需要再去和复杂的 SQL 语句打交道,只需简单的操作实体对象的属性和方法 。ORM 技术是在对象和关系之间提供了一条桥梁,前台的对象型数据和数据库中的关系型的数据通过这个桥梁来相互转化 。
开发步骤:
1、创建user表
2、pom.xml引入配置
3、编写全局配置文件:SqlMapConfig.xml
4、编写映射文件 :UserMapper.xml
5、编写dao代码:UserDao接口、UserDaoImpl实现类
6、POJO类
7、单元测试类
(1)创建user表
- CREATE TABLE `user` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `username` varchar(32) NOT NULL COMMENT '用户名称',
- `birthday` date DEFAULT NULL COMMENT '生日',
- `sex` char(1) DEFAULT NULL COMMENT '性别',
- `address` varchar(256) DEFAULT NULL COMMENT '地址',
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8;
-
- /*Data for the table `user` */
-
- insert into `user`(`username`,`birthday`,`sex`,`address`) values ('王五',NULL,'2',NULL),(10,'张三','2014-07-10','1','北京市');
(2)pom.xml引入配置
- <dependencies>
- <!-- mybatis依赖 -->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>3.4.6</version>
- </dependency>
- <!-- mysql依赖 -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.35</version>
- </dependency>
-
- <!-- 单元测试 -->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- </dependency>
- </dependencies>
(3)编写全局配置文件:SqlMapConfig.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>
- <!-- mybatis 数据源环境配置 -->
- <!-- 映入数据库配置文件 -->
- <properties resource="db.properties"></properties>
- <environments default="development">
- <environment id="development">
- <!-- 配置数据源信息 -->
- <transactionManager type="JDBC" />
- <dataSource type="POOLED">
- <property name="driver" value="${db.driver}" />
- <property name="url" value="${db.url}" />
- <property name="username" value="${db.username}" />
- <property name="password" value="${db.password}" />
- </dataSource>
- </environment>
- </environments>
-
- <!-- 映射文件加载 -->
- <mappers>
- <mapper resource="mapper/UserMapper.xml" />
- </mappers>
- </configuration>
(4)编写映射文件 :UserMapper.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="user">
- <!-- select标签,封装了SQL语句信息、入参类型、结果映射类型 -->
- <select id="findUserById" parameterType="java.lang.Integer"
- resultType="com.zzq.mybatis.entity.User">
- SELECT * FROM user WHERE id = #{id}
- </select>
- </mapper>
(5)编写dao代码:UserDao接口、UserDaoImpl实现类
- package com.zzq.mybatis.dao;
-
- import com.zzq.mybatis.entity.User;
-
- public interface UserDao {
- public User findUserById(int id);
- }
- package com.zzq.mybatis.dao;
-
- import org.apache.ibatis.session.SqlSession;
- import org.apache.ibatis.session.SqlSessionFactory;
-
- import com.zzq.mybatis.entity.User;
-
- public class UserDaoImpl implements UserDao {
-
- private SqlSessionFactory sqlSessionFactory;
-
- // 注入sqlSessionFactory
- public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {
- this.sqlSessionFactory = sqlSessionFactory;
- }
-
- public User findUserById(int id) {
- // sqlsessionFactory工厂类去创建sqlsession会话
- SqlSession sqlSession = sqlSessionFactory.openSession();
- // sqlsession接口,使用它对数据库进行增删改查操作
- User user = sqlSession.selectOne("user.findUserById", id);
- return user;
- }
-
- }
(6)POJO类
-
- package com.zzq.mybatis.entity;
-
- import java.util.Date;
-
- public class User {
-
- private int id;
-
- private String username;
-
- private Date birthday;
-
- private String sex;
-
- private String address;
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public String getUsername() {
- return username;
- }
-
- public void setUsername(String username) {
- this.username = username;
- }
-
- public Date getBirthday() {
- return birthday;
- }
-
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
-
- public String getSex() {
- return sex;
- }
-
- public void setSex(String sex) {
- this.sex = sex;
- }
-
- public String getAddress() {
- return address;
- }
-
- public void setAddress(String address) {
- this.address = address;
- }
-
- @Override
- public String toString() {
- return "User [id=" + id + ", username=" + username + ", birthday=" + birthday + ", sex=" + sex + ", address="
- + address + "]";
- }
-
- }
(7)单元测试类
- package com.zzq.mybatis.test;
-
- import java.io.InputStream;
-
- import org.apache.ibatis.io.Resources;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.apache.ibatis.session.SqlSessionFactoryBuilder;
- import org.junit.Before;
- import org.junit.Test;
-
- import com.zzq.mybatis.dao.UserDao;
- import com.zzq.mybatis.dao.UserDaoImpl;
- import com.zzq.mybatis.entity.User;
-
- public class Test1 {
-
- private SqlSessionFactory sqlSessionFactory;
-
- @Before
- public void init() throws Exception{
- //加载全局配置文件
- String resource = "SqlMapConfig.xml";
- InputStream inputStream = Resources.getResourceAsStream(resource);
- // sqlsessionFactory需要通过sqlsessionFactoryBuilder读取全局配置文件信息
- sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- }
-
- @Test
- public void testFindUserById() {
-
- UserDao dao = new UserDaoImpl(sqlSessionFactory);
- User user = dao.findUserById(1);
- System.out.println(user);
- }
-
- }
(8)效果
mybatis执行步骤:
1、加载全局配置文件 SqlMapConfig.xml,通过Resources对象获取全局配置文件
2、通过SqlSessionFactoryBuilder构建会话工厂SqlSessionFactory
3、通过会话工厂SqlSessionFactory得到会话SqlSession,
4、通过SqlSession执行CRUP操作
5、通过SqlSession提交事物
6、通过SqlSession关闭会话
#{}和${}区别
区别1
#{} :相当于JDBC SQL语句中的占位符? (PreparedStatement)
${} :相当于JDBC SQL语句中的连接符合 + (Statement)
区别2
#{} : 进行输入映射的时候,会对参数进行类型解析(如果是String类型,那么SQL语句会自动加上’’)
${} :进行输入映射的时候,将参数原样输出到SQL语句中
区别3
#{} : 如果进行简单类型(String、Date、8种基本类型的包装类)的输入映射时,#{}中参数名称可以任意
${} :如果进行简单类型(String、Date、8种基本类型的包装类)的输入映射时,${}中参数名称必须是value
区别4
${} :存在SQL注入问题 ,使用OR 1=1 关键字将查询条件忽略
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。