java-mybatis环境搭建
首先在iml文件中
<packaging>jar</packaging> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.26</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> </dependencies>
在java目录中创建一个domain实现类User,实现Serializable接口
public class User implements Serializable { private Integer id; private String username; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; }
在rescources文件中创建SqlMapConfig.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> <!-- 配置环境--> <environments default="mysql"> <!-- 配置mysql环境--> <environment id="mysql"> <!-- 配置事务--> <transactionManager type="JDBC"></transactionManager> <!-- 配置连接池--> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/javaweb1"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <!-- 配置映射文件的位置--> <mappers> <mapper resource="dao/IUserDao.xml"></mapper> </mappers> </configuration>
在resources中建造一个dao文件再IUserDao.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"> <!--namespace确定方法在哪个Dao接口,id指的是哪个方法,resultType封装至哪--> <mapper namespace="dao.IUserDao"> <!-- 查询所有操作--> <select id="findAll" resultType="dao.IUserDao"> select * from web1; </select> </mapper>
编写持久层接口IUserDao
public interface IUserDao { /** * @return */ List<User> findAll(); }
编写测试类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class MybatisTest { /* 测试查询所有 */ @Test public void testFindall() throws Exception{ //1.读取配置文件,生成字节输入流 InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml"); //2.获取SqlSessionFactory SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); //3.获取sqlsession对象 SqlSession sqlSession = factory.openSession(); //4.获取dao的代理对象 IUserDao userDao = sqlSession.getMapper(IUserDao.class); //5执行查询所有方法 List<User> users = userDao.findAll(); for (User user:users){ System.out.println(user); } //释放资源 sqlSession.close(); in.close(); } }