目录结构:
![]()
1.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>
<typeAliases>
<typeAlias alias="User" type="com.feng.work.bean.User" />
</typeAliases>
<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/test?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 方法一:这个适用于sql写在xml配置文件上的
List<User> userList = sqlSession.selectList("com.feng.work.mapper.UserMapper111.getUser", User.class);
- com.feng.work.mapper.UserMapper111要和xml中namespace一致
<mapper resource="com/feng/work/mapper/UserMapper.xml" />
-->
<!-- 方法二:需要先注册UserMapper接口,再映射相应的xml配置
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList2 = mapper.getUser();
<mapper class="com.feng.work.mapper.UserMapper" />
<mapper resource="com/feng/work/mapper/UserMapper.xml" />
-->
<!-- 该方法实际同方法二,使用这个方法的时,需要将mapper接口和xml配置文件放在一起,且文件名一样,且xml配置的namespace要和mapper接口一样 -->
<package name="com.feng.work.mapper"/>
<!-- 这边的配置我有一个疑问就是为什么不能引入指定目录下所有的mapper文件:<mapper resource="com/feng/work/mapper/*.xml" />希望知道的朋友指点一下,在此先谢过各位 -->
</mappers>
</configuration>
2.UserTest.java
package com.feng.work.test;
import com.feng.work.bean.User;
import com.feng.work.mapper.UserMapper;
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.IOException;
import java.io.Reader;
import java.util.List;
/**
* Created by jarry on 2017/6/22.
*/
public class UserTest {
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader;
//读取配置文件,用配置文件创建SqlSessionFactory对象
static {
try {
reader = Resources.getResourceAsReader("mybatis/mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSession() {
return sqlSessionFactory;
}
public static void main(String[] args) {
SqlSession sqlSession = sqlSessionFactory.openSession();
//映射到配置文件中的sql方法,方法一
//List<User> userList = sqlSession.selectList("com.feng.work.bean.User.getUser", User.class);
//方法二,用接口的方式
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList2 = mapper.getUser();
for(int i = 0; i<userList2.size(); i++) {
System.out.println(userList2.get(i).getUsername());
}
sqlSession.close();
}
}
3.UserMapper.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="com.feng.work.mapper.UserMapper">
<select id="getUser" resultType="User">
select * from t_user
</select>
</mapper>