1、Mybaits核心对象SqlSession的作用:
(1)向SQL语句传入参数;
(2)执行SQl语句;
(3)获取执行SQL语句的结果;
(4)事务的控制;
2、核心配置文件(Configration.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> <!-- <settings> <setting name="useGeneratedKeys" value="false"/> <setting name="useColumnLabel" value="true"/> </settings> <typeAliases> <typeAlias alias="UserAlias" type="org.apache.ibatis.submitted.complex_property.User"/> </typeAliases> --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"> <property name="" value=""/> </transactionManager> <dataSource type="UNPOOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/><!-- 配置sql驱动 --> <property name="url" value="jdbc:mysql://localhost:3306/school"/><!-- 配置数据库地,其中school为数据名称 --> <!-- jdbc:mysql://127.0.0.1:3306/school --> <property name="username" value="root"/><!-- 配置数据库用户名 --> <property name="password" value="********"/><!-- 配置数据库密码--> </dataSource> </environment> </environments> <mappers> <mapper resource="com/sysker/config/Students.xml"/><!-- 需要导入的xml配置文件,一般用于配置表格数据表格 --> </mappers> </configuration>
3、获取SqlSession:
(1)通过配置文件获取数据库连接相关信息:
Reader reader = Resources.getResourceAsReader("com/sysker/config/Configuration.xml");
(2)通过配置信息构建SqlSessionFactory:
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
(3)通过sqlSessionFactory打开数据库会话:
session = sqlSessionFactory.openSession();
下面是完整代码:
package com.sysker.db; import java.io.IOException; import java.io.Reader; 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 DBAccess { public SqlSession getSqlSession() throws IOException{ SqlSession session =null; //通过配置文件获取数据库连接信息 Reader reader = Resources.getResourceAsReader("com/sysker/config/Configuration.xml"); //通过配置信息构建一个SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); //通过sqlsessionFactory打开一个数据库会话 session = sqlSessionFactory.openSession(); return session; } }
4、配置与数据表格相关的sql语句(Students.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="Studnets"> <resultMap type="com.sysker.beans.Students" id="StudentsResult"> <id column="id" jdbcType="INTEGER" property="id"/> <result column="name" jdbcType="VARCHAR" property="name"/> <result column="classname" jdbcType="VARCHAR" property="classname"/> <result column="sex" jdbcType="VARCHAR" property="sex"/> <result column="age" jdbcType="VARCHAR" property="age"/> </resultMap> <select id="getAllStudents" resultMap="StudentsResult"> SELECT id,name,classname,sex,age FROM student </select> </mapper>
5、通过Dao层来执行SQL操作:
package com.sysker.dao; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import org.apache.ibatis.session.SqlSession; import com.sysker.beans.Students; import com.sysker.db.DBAccess; import com.sysker.jdbc.DBContol; public class StudentDAO { public List<Students> getAllStudents(){ DBAccess dbAccess = new DBAccess(); List<Students> studentsList = new ArrayList<Students>(); SqlSession sqlSession = null; try { sqlSession = dbAccess.getSqlSession(); //通过sqlSession执行sql语句 studentsList = sqlSession.selectList("Students.getAllStudents"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(sqlSession!=null){ sqlSession.close(); } } return studentsList; } public static void main(String[] args) { StudentDAO studentDAO = new StudentDAO(); List<Students> studentsList = new ArrayList<Students>(); studentsList = studentDAO.getAllStudents(); for (Students students : studentsList) { System.out.println(students.toString()); } } }
时间: 2024-10-04 19:55:48