简单的Mybatis项目
1.配置Maven依赖
依赖
1 <dependencies> 2 <!-- mysql数据库驱动依赖 1 --> 3 <dependency> 4 <groupId>mysql</groupId> 5 <artifactId>mysql-connector-java</artifactId> 6 <version>8.0.19</version> 7 </dependency> 8 <!-- mybatis驱动 --> 9 <dependency> 10 <groupId>org.mybatis</groupId> 11 <artifactId>mybatis</artifactId> 12 <version>3.5.2</version> 13 </dependency> 14 <!-- JUnit是用于测试单元 --> 15 <dependency> 16 <groupId>junit</groupId> 17 <artifactId>junit</artifactId> 18 <version>4.13.1</version> 19 <scope>test</scope> 20 </dependency> 21 22 </dependencies>
2.配置环境文件mybatis-config.xml
注意: 每一个Mapper.XML都需要在核心配置里面注册
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <environments default="development"> 7 <environment id="development"> 8 <transactionManager type="JDBC"/> 9 <dataSource type="POOLED"> 10 <property name="driver" value="com.mysql.cj.jdbc.Driver"/> 11 <property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8"/> 12 <property name="username" value="root"/> 13 <property name="password" value="123456789"/> 14 </dataSource> 15 </environment> 16 </environments> 17 <mappers> 18 <mapper resource="com/zxy/dao/UserMapper.xml"/> 19 </mappers> 20 </configuration>
mapper不同配置方法(可以留印象):
1 下面两种方式注意: 1.接口和Mapper配置文件同名 2.接口和Mapper配置文件在同一个包下 2 <package name="com.zxy.dao"/> 3 <mapper class="com.zxy.dao.UserMapper"/>
3.编写MybatisUtils工具类
1 package com.zxy.utils; 2 3 4 import org.apache.ibatis.io.Resources; 5 import org.apache.ibatis.session.SqlSession; 6 import org.apache.ibatis.session.SqlSessionFactory; 7 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 8 9 import java.io.IOException; 10 import java.io.InputStream; 11 12 //sqlSessionFactory ----> sqlSession 13 public class MybatisUtils { 14 private static SqlSessionFactory sqlSessionFactory; 15 static { 16 try { 17 //1.获取sqlSessionFactory对象 18 String resource = "mybatis-config.xml"; 19 InputStream inputStream = Resources.getResourceAsStream(resource); 20 sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 21 } catch (IOException e) { 22 e.printStackTrace(); 23 } 24 } 25 //SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句 26 public static SqlSession getSqlSession(){ 27 return sqlSessionFactory.openSession(); 28 } 29 }
4.编写实体类User
记得数据库和实体类一一对应
1 package com.zxy.pojo; 2 3 public class User { 4 private int id; 5 private String name; 6 private String pwd; 7 8 public User() { 9 } 10 11 public User(int id, String name, String password) { 12 this.id = id; 13 this.name = name; 14 this.pwd = password; 15 } 16 17 public int getId() { 18 return id; 19 } 20 21 public void setId(int id) { 22 this.id = id; 23 } 24 25 public String getName() { 26 return name; 27 } 28 29 public void setName(String name) { 30 this.name = name; 31 } 32 33 public String getPassword() { 34 return pwd; 35 } 36 37 public void setPassword(String password) { 38 this.pwd = password; 39 } 40 41 @Override 42 public String toString() { 43 return "User{" + 44 "id=" + id + 45 ", name='" + name + '\'' + 46 ", password='" + pwd + '\'' + 47 '}'; 48 } 49 }
5.实体类UseDaor的接口(初学者适应 最好写成UserMapper)
1 package com.zxy.dao; 2 3 import com.zxy.pojo.User; 4 5 import java.util.List; 6 7 public interface UserDao { 8 List<User> getUserList(); 9 }
6.编写基于 XML的对于sql的映射语句UserMapper.xml
1、namespace绑定一个对应的Dao/Mapper接口
2,这个XML的文件一般写在Dao目录下在pom.xml中配置文件过滤规则 Xml资源文件无法导出,找不到Mapper.xml文件
点此连接可以得到配置代码
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 7 <mapper namespace="com.zxy.dao.UserDao"> 8 <select id="getUserList" resultType="com.zxy.pojo.User"> 9 select * from mybatis.user; 10 </select> 11 </mapper>
7.编写测试类
public class UserDaoTest { @Test public void test(){ //1.获取sqlSession对象 SqlSession sqlSession = MybatisUtils.getSqlSession(); //2. UserDao userDao = sqlSession.getMapper(UserDao.class); List<User> userList = userDao.getUserList(); for ( User user :userList) { System.out.println(user); } //关闭SqlSession sqlSession.close(); } }