第一步先用类加载器加载解析配置文件
第二步创建sqlsessionfactory工厂
第三步创建可以执行映射文件中xml的SQL的SqlSession
第四步执行SQL
关闭session
package com.example.demo;
import java.io.IOException;import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class TestMb {
public static void main(String[] args) throws IOException {
//mybatis的配置文件
String resource = "conf.xml";
//使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
InputStream is = TestMb.class.getClassLoader().getResourceAsStream(resource);
//构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
//使用MyBatis提供的Resources类加载mybatis的配置文件(它也加载关联的映射文件)
//Reader reader = Resources.getResourceAsReader(resource);
//构建sqlSession的工厂
//SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
//创建能执行映射文件中sql的sqlSession
SqlSession session = sessionFactory.openSession();
/**
* 映射sql的标识字符串,
* me.gacl.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值,
* getUser是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL
*/
String statement = "com.example.demo.userMapper.getUser";
//映射sql的标识字符串
//执行查询返回一个唯一user对象的sql
User user = session.selectOne(statement, 1);
System.out.println(user.toString());
String statementAge = "com.example.demo.userMapper.getUserByAge";
List<User> userAge = session.selectList(statementAge,27);
System.out.println(Arrays.asList(userAge.toString()));
// String statementInt = "com.example.demo.userMapper.insertUser";
// User userInt = new User();
// userInt.setAge(20);
// userInt.setId(4);
// userInt.setName("Mike");
// session.insert(statementInt, userInt);
//
String statementFindAll = "com.example.demo.userMapper.findAll";
List<User> userAll = session.selectList(statementFindAll);
System.out.println(Arrays.asList(userAll.toString()));
// String statementFindUp = "com.example.demo.userMapper.updateUser";
// User userUpt = new User();
// userUpt.setName("Jack");
// userUpt.setId(1);
// session.update(statementFindUp, userUpt);
//
// session.commit();
session.close();
}
}
package com.example.demo;
public class User {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
<?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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀)
-->
<mapper namespace="com.example.demo.userMapper">
<!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,id属性值必须是唯一的,不能够重复
使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型
resultType="me.gacl.domain.User"就表示将查询结果封装成一个User类的对象返回
User类就是users表所对应的实体类
-->
<!--
根据id查询得到一个user对象
-->
<select id="getUser" parameterType="int"
resultType="com.example.demo.User">
select * from users where id=#{id}
</select>
<select id="getUserByAge" parameterType="int"
resultType="com.example.demo.User">
select * from users where age=#{age}
</select>
<select id="findAll" resultType="com.example.demo.User">
select * from users
</select>
<insert id="insertUser">
insert into users(id,name,age) values(#{id},#{name},#{age})
</insert>
<update id="updateUser">
update users set name=#{name} where id=#{id}
</update>
</mapper>
<?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/my" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 注册userMapper.xml文件,
userMapper.xml位于me.gacl.mapping这个包下,所以resource写成me/gacl/mapping/userMapper.xml-->
<mapper resource="com/example/demo/userMapper.xml"/>
</mappers>
</configuration>