1、第一个Mybatis程序
思路:搭建环境-->导入Mybatis-->编写代码-->测试!
2、搭建环境
搭建数据库:
CREATE DATABASE mybatis; USE mybatis; CREATE TABLE user ( `id` INT(20) NOT NULL PRIMARY KEY, `name` VARCHAR(30) DEFAULT NULL, `pwd` VARCHAR(30) DEFAULT NULL )ENGINE=INNODB DEFAULT CHARSET=utf8; INSERT INTO user(`id`, `name`, `pwd`) VALUES (1, 'allen','123456'), (2, '张三', '123456'), (3, '李四', '123890');
搭建项目:
1、新建一个普通的maven项目
2、删除src目录
3、导入Maven依赖
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.19</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.4</version> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency>
4、创建一个maven的子模块,mybatis-01
编写mybatis核心配置文件,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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8"/> <property name="username" value="root"/> <property name="password" value="li980314"/> </dataSource> </environment> </environments> </configuration>
编写mybatis工具类,MybatisUtils:
package com.allen.utils; 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.InputStream; public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory = null; static{ try { //获取SqlSessionFactory对象 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
5、编写代码
实体类,User.java:
package com.allen.pojo; //实体类 public class User { private int id; private String name; private String pwd; public User() { } public User(int id, String name, String pwd) { this.id = id; this.name = name; this.pwd = pwd; } 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 String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", pwd='" + pwd + '\'' + '}'; } }
Dao接口,UserDao.java:
package com.allen.dao; import com.allen.pojo.User; import java.util.List; public interface UserDao { List<User> getUserList(); }
Mapper映射文件,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"> <!-- namespace="绑定一个dao/mapper接口" --> <mapper namespace="com.allen.dao.UserDao"> <!-- select查询语句 --> <select id="getUserList" resultType = "com.allen.pojo.User"> select * from mybatis.user; </select> </mapper>
在Mybatis核心配置文件mybatis-config.xml的configuration节点下,增加mapper注册配置:
<!-- 每一个Mapper.xml都需要在Mybatis核心配置文件中注册 --> <mappers> <mapper resource="com/allen/dao/UserMapper.xml" /> </mappers>
编写jUnit测试类:
package com.allen.dao; import com.allen.pojo.User; import com.allen.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; public class UserDaoTest { @Test public void Test(){ //第一步:获得sqlSession对象 SqlSession sqlSession = MybatisUtils.getSqlSession(); //方式一:getMapper UserDao userDao = sqlSession.getMapper(UserDao.class); List<User> userList = userDao.getUserList(); //方式二: //List<User> userList = sqlSession.selectList("com.allen.dao.UserDao.getUserList"); for (User user : userList) { System.out.println(user); } //关闭sqlSession sqlSession.close(); } }
6、测试
可能会遇到的问题java.lang.ExceptionInInitializerError
是因为我们在java文件夹下写了xml文件,maven没法导出,需要在pom.xml文件下配置:
<!-- 在build中配置resources,来防止我们资源导出失败的问题 --> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
再次运行项目,查出数据: