mybatis的xml配置
总配置文件 (maven)放在src/main/resource目录下面
log4j日志配置文件不写了
<?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="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///mybatis?characterEncoding=utf-8&userSSL=false&serverTimezone=GMT%2B8"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/guang/dao/IUserDao.xml"></mapper>
</mappers>
</configuration>
映射配置文件 放在resource目录 跟代码包的路径一样创建一个多级目录
<?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.guang.dao.IUserDao">
<!-- select all -->
<select id="findAll" resultType="com.guang.domain.User">
select * from user
</select>
</mapper>
mybatis的快速入门
写一个单元测试类
package com.guang.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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 org.junit.Test;
import com.guang.dao.IUserDao;
import com.guang.domain.User;
快速入门信息
public class IUserDaoTest {
@Test
public void testFindAll() throws IOException {
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
SqlSession session = factory.openSession();
IUserDao userdao = session.getMapper(IUserDao.class);
List<User> users = userdao.findAll();
for(User user : users) {
System.out.println(user);
}
}
}