MyBatis 学习
1:JDBC开发存在哪些缺点?
1):从数据库中查询学生信息,将学生信息封装为学生对象,将学生对象放到List集合中,进行展示。
package com.fangjun.jdbc;
import com.fangjun.javabean.Student;
import java.sql.*;
import java.util.ArrayList;
import java.util.ResourceBundle;
public class Test05 {
public static void main(String[] args) {
Connection connection=null;
Statement statement =null;
ResultSet resultSet=null;//结果集对象
ResourceBundle jdbc = ResourceBundle.getBundle("jdbc");
String driver = jdbc.getString("driver");
String url = jdbc.getString("url");
String user = jdbc.getString("user");
String pass = jdbc.getString("pass");
ArrayList<Student> students = new ArrayList<>();
try {
Class.forName(driver);
connection=DriverManager.getConnection(url,user,pass);
statement=connection.createStatement();
String sql="select id,name,birth from tbl_student";
resultSet = statement.executeQuery(sql);
while (resultSet.next()){
String id = resultSet.getString(1);
String name = resultSet.getString(2);
String birth = resultSet.getString(3);
Student student = new Student();
student.setId(id);
student.setName(name);
student.setBirth(birth);
students.add(student);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if (resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//在这里拿着list集合去展示
for (Student student:students){
System.out.println(student);
}
}
}
缺点:
1)重复代码太多,开发效率降低;
以下代码反复从结果集中取数据,反复调用对象的Set和Get方法给对象的属性赋值。这个过程完全可以使用反射机制替代,mybatis框架就是别人提前写好了java代码。他封装了jdbc代码,利用反射机制,帮助我们创建java对象。
while (resultSet.next()){
//反复调用同一个方法,只有参数不同
String id = resultSet.getString(1);
String name = resultSet.getString(2);
String birth = resultSet.getString(3);
Student student = new Student();
//反复调用同一个方法,只有参数不同
student.setId(id);
student.setName(name);
student.setBirth(birth);
students.add(student);
}
2)jdbc中编写在java代码,SQL语句不支持配置。语句可能后期需要调优以及修改。在java程序中编写的SQL语句,修改后,后期需要重新修改,会导致代码的重新编译与部署。违背OCP原则。
2:Mybatis框架的概述
框架的表现形式就是别人写好的一堆class文件,这些字节码通常被打包成了jar包,使用这个框架只需要将jar包引入到classpath中就可以。使用框架的目的是提高开发效率。
3:Mybatis的使用
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>
<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/wkcto"/>
<property name="username" value="root"/>
<property name="password" value="333"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="SqlMapper.xml"/>
</mappers>
</configuration>
SqlMapper.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="dd">
<!--id具有唯一性,代表SQL语句,id具有唯一性,代表了这条sql语句,将来这个id是需要“拷贝”到java程序当中的。查询结果集的列名要和javabean的属性名对应上,不对应的时候使用as关键字起别名。 通过resultType 诉mybatis,最终查询出的结果集赋值到javabean的哪个对象上面-->
<select id="sdsdad" resultType="com.javabean.Student">
select id as sid,name as sname,birth as sbirth from tbl_student
</select>
</mapper>
对应的java程序
package com.fangjun;
import com.javabean.Student;
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.io.InputStream;
import java.util.List;
public class mybaits001 {
public static void main(String[] args) {
String resource = "mybatis-config.xml";
InputStream inputStream = null;
SqlSession sqlSession = null;
try {
inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//事务自动提交机制关闭,等同于connection.setAutoCommit(false);
sqlSession = sqlSessionFactory.openSession();
//执行业务逻辑
List<Student> students = sqlSession.selectList("sdsdad");
for (Student student:students){
System.out.println(student.getSid()+","+student.getSbirth()+","+student.getSname());
}
//没有出现异常,则事务结束,进行提交
sqlSession.commit();
} catch (Exception e) {
if (sqlSession != null) {
sqlSession.rollback();
}
e.printStackTrace();
}finally {
if (sqlSession!=null){
sqlSession.close();
}
}
}
}