思路:搭建环境–>导入Mybatis–>编写代码–>测试
create table mybatis; use mybatis; create table user ( id int(20) not null primary key, username varchar(30) default null, pwd varchar(30) default null ) engine=INNODB DEFAULT CHARSET=utf8; insert into user(id,username,pwd) values (1,'admin','123456'), (2,'张三','123456'), (3,'李四','123456');
1、新建一个普通的maven项目
2、删除src目录
3、导入maven依赖
<dependencies> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
<?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/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> </configuration>
public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static{ try { // 使用Mybatis第一步:获取SqlSessionFactory对象 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } //既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。 // SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。 public static SqlSession getSqlSession() { return sqlSessionFactory.openSession(); } }
public class User { private int id; private String username; private String pwd; public User() { } public User(int id, String username, String pwd) { this.id = id; this.username = username; this.pwd = pwd; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", pwd='" + pwd + '\'' + '}'; } }
public interface UserDao { List<User> getUserList(); }
<?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.lyh.dao.UserDao"> <!--id对应UserDao中的方法名--> <select id="getUserList" resultType="com.lyh.pojo.User"> select * from mybatis.user </select> </mapper>
public class UserDaoTest { @Test public void test(){ //第一步:获得sqlSession对象 SqlSession sqlSession = MybatisUtils.getSqlSession(); //执行SQL //方式一:getMapper UserDao mapper = sqlSession.getMapper(UserDao.class); List<User> userList = mapper.getUserList(); //方式二:不推荐使用 // List<User> userList = sqlSession.selectList("com.lyh.dao.UserDao.getUserList"); for (User user: userList){ System.out.println(user); } // 关闭SqlSession sqlSession.close(); } }
注意可能会用到的问题:
错误一:配置文件没有注册
org.apache.ibatis.binding.BindingException: Type interface com.lyh.dao.UserDao is not known to the MapperRegistry.
<!--每一个Mapper.xml都需要在Mybatis核心配置文件中注册--> <mappers> <mapper resource="com/lyh/dao/UserMapper.xml/"></mapper> </mappers>
错误二:Maven导出资源问题
Could not find resource com/lyh/dao/UserMapper.xml/
解决方案:
<!--在build中配置resources,来防止我们资源导出失败的问题--> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build>