初入Mybatis
项目结构
数据库设计
config放到类路径下
类路径:Java 类路径告诉 java 解释器和 javac 编译器去哪里找它们要执行或导入的类。
config下的文件
log4j.xml可以从网上直接复制
dbconfig.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8 username=root password=123
recourse.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="dbconfig.properties"></properties> <environments default="development"> <environment id="development"><!-- 开发者模式 --> <transactionManager type="JDBC"/><!-- 事务管理器 --> <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> </environments> <!-- 对应实体类的配置文件 --> <mappers> <mapper resource="com/mybatis/entity/Employee.xml"/> </mappers> </configuration>
Employee.java
package com.mybatis.entity; //员工实体类,负责和数据库中的表做映射 /*有一个类,所有属性都是私有的并且提供公有的setter getter方法,就是java bean * * */ public class Employee { private String id; private String name; private char gender; private String address; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public char getGender() { return gender; } public void setGender(char gender) { this.gender = gender; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
Employee.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"> <!-- 每个实体类都对应着Mybatis的一个命名空间。namespace属性值相当于id,不允许重复 --> <!-- 命名空间,一般情况下,在当前空间中声明的是,实体类对应全路径 --> <mapper namespace="com.mybatis.entity.Employee"> <!-- select标签执行查询 结果集类型(resultType)是实体类对象 方法传入参数类型(parameterType) #{}相当于占位符,代表传过来的参数 --> <select id="selectOne" parameterType="String" resultType="com.mybatis.entity.Employee"> select * from emp where id = #{id} </select> </mapper>
Test.java
package com.mybatis.test; import java.io.IOException; import java.io.InputStream; 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; import com.mybatis.entity.Employee; public class Test { public void wan(){ try { //获得当前mybatis总的配置文件路径 String resource = "recourse.xml"; //获得当前配置文件的输入流 Reader reader = Resources.getResourceAsReader(resource); //通过流对象创建SqlSessionFactory对象,数据库会话工厂 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); //通过数据库会话工厂开启跟数据库的一次会话 SqlSession sqlSession = sqlSessionFactory.openSession(); //通过sqlSession对象,执行sql语句,用完后关闭对象 //所有的数据库操作都被封装到sqlSession对象 /*指定具体执行哪条sql语句:通过命名空间加id的方式 * * 传入参数 * */ Employee xm= sqlSession.selectOne("com.mybatis.entity.Employee.selectOne","1"); System.out.println(xm.getAddress()); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { Test test = new Test(); test.wan(); } }
运行结果