一、目录结构

 

二、代码

1、dao/UserDao

 1 package cn.bijian.dao;
 2 
 3 import cn.bijian.model.User;
 4 import org.apache.ibatis.annotations.Select;
 5 
 6 import java.util.List;
 7 
 8 public interface UserDao {
 9     /*
10     查询所有User
11      */
12 //    @Select("select * from user")
13     List<User> findAll();
14 
15 }

2、model/User

 1 package cn.bijian.model;
 2 
 3 import java.util.Date;
 4 
 5 public class User {
 6     private Integer id;
 7     private String username;
 8     private Date birthday;
 9     private String sex;
10     private String address;
11 
12     public Integer getId() {
13         return id;
14     }
15 
16     public void setId(Integer id) {
17         this.id = id;
18     }
19 
20     public String getUsername() {
21         return username;
22     }
23 
24     public void setUsername(String username) {
25         this.username = username;
26     }
27 
28     public Date getBirthday() {
29         return birthday;
30     }
31 
32     public void setBirthday(Date birthday) {
33         this.birthday = birthday;
34     }
35 
36     public String getSex() {
37         return sex;
38     }
39 
40     public void setSex(String sex) {
41         this.sex = sex;
42     }
43 
44     public String getAddress() {
45         return address;
46     }
47 
48     public void setAddress(String address) {
49         this.address = address;
50     }
51 
52     @Override
53     public String toString() {
54         return "User{" +
55                 "id=" + id +
56                 ", username='" + username + '\'' +
57                 ", birthday=" + birthday +
58                 ", sex='" + sex + '\'' +
59                 ", address='" + address + '\'' +
60                 '}';
61     }
62 }

3、dao/UserDao.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 <mapper namespace="cn.bijian.dao.UserDao">
6     <select id="findAll" resultType="cn.bijian.model.User">
7         select * from User;
8     </select>
9 </mapper>

4、SqlMapConfig.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="mysql">
 7         <environment id="mysql">
 8             <!--JDBC 事务管理器被用作当应用程序负责管理数据库连接的生命周期(提交、回退等等)的时候。当你将
 9             TransactionManager 属性设置成 JDBC,MyBatis 内部将使用 JdbcTransactionFactory 类创建
10             TransactionManager。例如,部署到 Apache Tomcat 的应用程序,需要应用程序自己管理事务。-->
11             <transactionManager type="JDBC"></transactionManager>
12             <dataSource type="POOLED">  <!--连接池-->
13                 <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
14                 <property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC"/>
15                 <property name="username" value="root"/>
16                 <property name="password" value="123456"/>
17             </dataSource>
18         </environment>
19     </environments>
20 
21     <mappers>
22 <!--        <mapper resource="cn/bijian/dao/UserDao.xml"></mapper>-->
23        <mapper class="cn.bijian.dao.UserDao"></mapper>  <!--用注解方式,需移除dao.xml映射配置-->
24     </mappers>
25 </configuration>

5、MybatisTest

 1 package cn.bijian.test;
 2 
 3 
 4 import cn.bijian.dao.UserDao;
 5 import cn.bijian.model.User;
 6 import org.apache.ibatis.io.Resources;
 7 import org.apache.ibatis.session.SqlSession;
 8 import org.apache.ibatis.session.SqlSessionFactory;
 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
10 
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.util.List;
14 
15 
16 public class MybatisTest {
17     public static void main(String[] args) throws IOException {
18         // 1.获取配置文件
19         InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
20         // 2.创建SqlSessionFactory的构建者对象
21         SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
22         // 3.根据构建者对象创建SqlSessionFactory
23         SqlSessionFactory sqlSessionFactory = builder.build(inputStream);
24         // 4.根据sqlSessionFactory生产SqlSession对象
25         SqlSession session = sqlSessionFactory.openSession();
26         // 5.根据SqlSession创建Dao接口的代理对象
27         UserDao userDao = session.getMapper(UserDao.class);
28         List<User> users = userDao.findAll();
29         for (User user:users) {
30             System.out.println(user);
31         }
32         session.close();
33         inputStream.close();
34     }
35 }

 

posted on 2021-10-27 19:47  晨曦生辉耀匕尖  阅读(29)  评论(0编辑  收藏  举报