1、为什么要学框架
- 框架是一个应用程序的半成品,提供了项目中的公共组件和实现,留给了程序员标准的API
- 常用的框架:Struts2、HIbernate、MyBatis、Spring、SpringMvc、SpringBoot、SpringCloud、
二、Mybatis:
- 简介: ORM(对象关系映射)思想的实现 :Mybatis 、Hibernate
- 官网:https://mybatis.org/
- JDBC区别: 几乎消除了JDBC原有的连接、参数设置等等很多的重复代码
- jar包下载
编程步骤:
1. 创建工程、添加Mybatis依赖包(不选择maven的方式)
mybatis-3.5.5 或者mybatis-3.4.6
新建java project 项目来创建mybatis项目—》build path ----》config build path–>
2. 创建MyBatis的配置文件(mybatis-config.xml)
创建一个source folder文件
在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>
<!--1. 配置数据源driver、url、username、password -->
<!-- resource优先级高-->
<properties resource="database.properties">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!-- 连接地址 -->
<property name="url" value="jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8" />
<!-- 用户名 -->
<property name="username" value="root"/>
<!-- 密码 -->
<property name="password" value="root"/>
</properties>
<!-- 2. 配置MyBatis运行全局设置 -->
<settings>
<setting name="lazyLoadingEnabled" value="false"/>
</settings>
<!--3. 配置别名 -->
<typeAliases>
<!-- <typeAlias type="com.oupeng.pojo.User" alias="u" /> -->
<package name="com.oupeng.pojo"/>
</typeAliases>
<!-- 4. 配置开发环境-->
<environments default="development">
<!-- 配置第一个开发环境 -->
<environment id="development">
<!--事务的管理 -->
<transactionManager type="JDBC"></transactionManager>
<!-- 数据源的管理 -->
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
<!-- 配置测试开发环境 -->
<environment id="test">
<transactionManager type=""></transactionManager>
<dataSource type=""></dataSource>
</environment>
</environments>
<!-- 加载接口的映射文件 -->
<mappers>
<mapper resource="com/oupeng/user/dao/UserMapper.xml"/>
</mappers>
</configuration>
3. 创建应用接口
package com.oupeng.user.dao;
import java.util.List;
public interface UserMapper {
//统计当前用户表中的记录数
public int count();
//查询当前所有的用户记录
public List getUserList();
}
接口的映射文件(.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">
<!-- nameSpace相当于一个文件夹,用来找映射文件 接口 里面id和接口的方法名相同;resultType表示方法返回值-->
<mapper namespace="com.oupeng.user.dao.UserMapper">
<select id="count" resultType="Integer">
select count(1) as count from smbms_user
</select>
</mapper>
4. 创建持久化类(pojo)
package com.oupeng.pojo;
import java.util.Date;
public class User {
private String userCode; //账号
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
private String userName; //姓名
private String userPassword; //密码
private Integer gender; //性别
private Date birthday; //生日
private String phone; //联系方式
private String address; //联系地址
private Integer userRole;//角色编号
private Date creationDate ;//创建日期
private Integer createdBy; //创建人
private Integer modifyBy; //修改人
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
private Date modifyDate; //修改日期
private String idPicPath; //身份照
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getUserRole() {
return userRole;
}
public void setUserRole(Integer userRole) {
this.userRole = userRole;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Integer getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
public String getIdPicPath() {
return idPicPath;
}
public void setIdPicPath(String idPicPath) {
this.idPicPath = idPicPath;
}
public String getWorkPicPath() {
return workPicPath;
}
public void setWorkPicPath(String workPicPath) {
this.workPicPath = workPicPath;
}
private String workPicPath;// 证件照
}
5. 创建测试程序
单元测试类
build path ----》config build path–>
核心接口和类:
SqlSessionFactoryBuilder:作用域,用过即丢(建议作用域在方法内部)
SqlSessionFactory 单例模式,在整个项目应用中只有一个
SqlSession :线程级,非线程安全
package com.oupeng;
import java.io.IOException;
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 TestUserMapper {
public static void main(String[] args) {
// 加载MyBatis的配置文件
String str = "mybatis-config.xml";
SqlSession sqlSession = null;
InputStream inputStream;
int count = 0;
try {
// 读取MyBatis的配置文件
inputStream = Resources.getResourceAsStream(str);
// 创建SqlSessionFactory工厂
SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
// 创建SqlSession
sqlSession = sqlSessionFactory.openSession();
// 统计当前用户的记录数
count = Integer.parseInt(sqlSession.selectOne("com.oupeng.user.dao.UserMapper.count").toString());
System.out.println("当前系统用户数为:" + count + "人...");
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭sqlSession
if(sqlSession!=null){
sqlSession.close();
}
}
}
}
执行结果
当前系统用户数为:14人...
MyBatis的优缺点:
优点 :小巧灵活,易上手,大量减少了JDBC编程的代码
缺点:要大量的编写sql语句,要求开发人员对sql要熟悉。