1.什么是MyBatis?
mybatis是一个基于ORM(对象关系映射)的数据访问层框架。
2.何为框架呢?
框架本质还是java程序,只不过框架是被一些牛人将原始及底层代码进行封装,将封装后的程序打包提供开发人员使用。帮助开发者提高开发的效率,同时也能提升项目的性能。
数据访问层–通常我们在做项目的时候,会把一个项目分成3个部分,这3个部分分别是:
2.1 控制层[web层]----用来做数据的导航【Servlet、Struts2、SpringMVC】
2.1 业务层----用来处理相关功能的具有实现业务。【Spring】
2.3 数据访问层[数据持久层]—用来访问数据库数据。【JDBC、Hibernate、MyBatis】
早期JavaWeb的3大框架—SSH[Struts2-Spring-Hibernate] 现在流行的JavaWeb的3大框架—SSM[SpringMVC-Spring-MyBatis]
3. 什么是ORM?
ORM—对象关系映射
我们在访问数据库的时候所编写的都是Java程序,Java程序只认识Java对象,而我们所访问的数据库大多数都是关系型数据库,那么这时Java程序要想访问关系型数据库,那么就需要将Java对象转换成关系型数据,才能被数据库认识。这时我们可以认为一个Java类就是关系型数据库中的一张数据表,Java类中的成员变量是数据库表中的一个列,Java类创建的Java对象就是数据库表中的一行记录。这时将Java对象对应成为数据库表记录的过程就是对象关系映射。
4. 为什么要使用MyBatis?
4.1 为了简化数据库访问操作,提高开发的效率,提升项目的性能,增加程序的可维护性。
4.2. 当我们使用Java程序控制Java对象的时候,数据库中的数据表记录会随之变化。(将原来通过java程序访问数据库表的操作,简化成通过java程序访问java对象)。
5.简单使用MyBatis框架开发一个用户信息系统
5.1 创建数据库表结构
create table tb_student(
stuid int primary key auto_increment,
stuname varchar(30),
stupassword varchar(20),
stuage int,
stuaddress varchar(30)
);
5.2 使用IDEA创建普maven项目,完善项目结构
5.3 pox.xml文件导入依赖
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
5.4 根据数据库表结构创建java实体类
package com.qing.bean;
public class StudentBean {
private int stuid;
private String stuname;
private String stupassword;
private int stuage;
private String stuaddress;
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getStupassword() {
return stupassword;
}
public void setStupassword(String stupassword) {
this.stupassword = stupassword;
}
public int getStuage() {
return stuage;
}
public void setStuage(int stuage) {
this.stuage = stuage;
}
public String getStuaddress() {
return stuaddress;
}
public void setStuaddress(String stuaddress) {
this.stuaddress = stuaddress;
}
}
5.5 在src/main/resources中编写数据库资源文件 mydate.properties
mydriver = com.mysql.jdbc.Driver
myurl = jdbc:mysql://127.0.0.1:3306/test
myusername = root
mypassword = 123456
5.6 创建数据库访问接口
package com.qing.mapper;
import com.qing.bean.StudentBean;
import java.util.List;
public interface StudentMapper {
//添加信息
boolean insertStudent(StudentBean studentBean);
//修改信息
boolean updateStudentId(int stuid);
//删除信息
boolean deleteStudent(int stuid);
//根据id查询
StudentBean seleteStudentId(int stuid);
//查询所有
List<StudentBean> selectStudent();
}
5.7 在src/main/resources中编写sql映射文件 StudentMapper.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="com.qing.mapper.StudentMapper">
<insert id="insertStudent" parameterType="com.qing.bean.StudentBean">
insert into tb_student values (null,#{stuname},#{stupassword},#{stuage},#{stuaddress});
</insert>
<update id="updateStudent" parameterType="int">
update tb_student set stuname=#{stuname},stupassword=#{stupassword},stuage=#{stuage},stuaddress=#{stuaddress} where stuid=#{stuid};
</update>
<delete id="deleteStudent" parameterType="int">
delete from tb_student where stuid=#{stuid};
</delete>
<resultMap id="studentMap" type="com.qing.bean.StudentBean">
<id column="stuid" property="stuid"></id>
<result column="stuname" property="stuname"></result>
<result column="stupassword" property="stupassword"></result>
<result column="stuage" property="stuage"></result>
<result column="stuaddress" property="stuaddress"></result>
</resultMap>
<select id="seleteStudentId" parameterType="int" resultMap="studentMap">
select * from tb_student where stuid=#{stuid};
</select>
<select id="selectStudent" resultMap="studentMap">
select * from tb_student;
</select>
</mapper>
5.8 在src/main/resources中创建 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>
<!-- 配置引入数据连接字符串的资源文件 -->
<properties resource="mydate.properties"></properties>
<!-- 配置mybatis默认的连接数据库环境 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${mydriver}"></property>
<property name="url" value="${myurl}"></property>
<property name="username" value="${myusername}"></property>
<property name="password" value="${mypassword}"></property>
</dataSource>
</environment>
</environments>
<!-- 配置mybatis数据访问接口的SQL映射文件路径 -->
<mappers>
<mapper resource="StudentMapper.xml"></mapper>
</mappers>
</configuration>
5.9 测试
package com.qing.test;
import com.qing.bean.StudentBean;
import com.qing.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.util.List;
public class TestMain {
/**
* 添加信息
*/
public static void insertStudent() {
//定义SqlSession对象
SqlSession sqlSession = null;
try {
//通过SqlSessionFactoryBuilder类创建出SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = new
SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
//从SqlSessionFactory中取得一个SqlSession对象
sqlSession = sqlSessionFactory.openSession();
//通过数据访问接口调用添加方法
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
StudentBean studentBean = new StudentBean();
studentBean.setStuname("烤红薯");
studentBean.setStupassword("111111");
studentBean.setStuage(22);
studentBean.setStuaddress("长丰国际");
studentMapper.insertStudent(studentBean);
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlSession.close();
}
}
/**
* 修改信息
* @param stuid
*/
public static void updateStudentId(int stuid) {
SqlSession sqlSession = null;
try {
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
sqlSession = sqlSessionFactory.openSession();
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
StudentBean studentBean = new StudentBean();
studentBean.setStuid(stuid);
studentBean.setStuname("地瓜");
studentBean.setStupassword("123321");
studentBean.setStuage(122);
studentBean.setStuaddress("曲江");
studentMapper.updateStudentId(stuid);
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlSession.close();
}
}
/**
* 删除信息
* @param stuid
*/
public static void deleteStudentId(int stuid) {
SqlSession sqlSession = null;
try {
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
sqlSession = sqlSessionFactory.openSession();
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
StudentBean studentBean = new StudentBean();
studentBean.setStuid(9);
studentMapper.deleteStudent(stuid);
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlSession.close();
}
}
/**
* 根据id查询信息
* @param stuid
*/
public static void selectStudentId(int stuid) {
SqlSession sqlSession = null;
try {
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
sqlSession = sqlSessionFactory.openSession();
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
StudentBean studentBean = studentMapper.seleteStudentId(8);
System.out.println(studentBean.getStuname() + "\t" + studentBean.getStuaddress());
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlSession.close();
}
}
/**
* 查询所有信息
*/
public static void selectStudent() {
SqlSession sqlSession = null;
try {
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
sqlSession = sqlSessionFactory.openSession();
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
List<StudentBean> studentlist = studentMapper.selectStudent();
for (StudentBean studentBean : studentlist) {
System.out.println(studentBean.getStuname() + "\t" + studentBean.getStupassword() + "\t" + studentBean.getStuage() + "\t" + studentBean.getStuaddress());
}
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlSession.close();
}
}
/**
* 测试
* @param args
*/
public static void main(String[] args) {
//insertStudent();
updateStudentId(4);
//deleteStudentId(9);
//selectStudentId(8);
//selectStudent();
}
}