黑马MyBatis入门day1

 1 package com.itheima.domain;
 2 
 3 /*
 4 CREATE TABLE `user` (
 5   `id` int(11) NOT NULL AUTO_INCREMENT,
 6   `username` varchar(50) DEFAULT NULL,
 7   `password` varchar(50) DEFAULT NULL,
 8   `birthday` bigint(20) DEFAULT NULL,
 9   PRIMARY KEY (`id`)
10 ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
11  */
12 public class User {
13     private int id;
14     private String username;
15     private String password;
16     private Long birthday;
17 
18     public User() {
19     }
20 
21     public int getId() {
22         return id;
23     }
24 
25     public void setId(int id) {
26         this.id = id;
27     }
28 
29     public String getUsername() {
30         return username;
31     }
32 
33     public void setUsername(String username) {
34         this.username = username;
35     }
36 
37     public String getPassword() {
38         return password;
39     }
40 
41     public void setPassword(String password) {
42         this.password = password;
43     }
44 
45     public Long getBirthday() {
46         return birthday;
47     }
48 
49     public void setBirthday(Long birthday) {
50         this.birthday = birthday;
51     }
52 
53     @Override
54     public String toString() {
55         return "User{" +
56                 "id=" + id +
57                 ", username='" + username + '\'' +
58                 ", password='" + password + '\'' +
59                 ", birthday=" + birthday +
60                 '}';
61     }
62 }
User
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC
 3         "-//mybatis.org//DTD Mapper 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="com.itheima.mapper.UserMapper">
 6     <!--查询全部-->
 7     <select id="findAll" resultType="User">
 8         select * from user
 9     </select>
10 
11     <!--根据id进行查询-->
12     <select id="findById" resultType="User" parameterType="int">
13         select * from user where id=#{id}
14     </select>
15 
16     <!--增-->
17     <insert id="add" parameterType="User">
18         insert into user(username, password) values(#{username}, #{password})
19     </insert>
20 
21     <!--删-->
22     <delete id="delete" parameterType="java.lang.Integer">
23         delete from user where id=#{id}
24     </delete>
25 
26     <!--改-->
27     <update id="update" parameterType="User">
28         update user set username=#{username}, password=#{password} where id=#{id}
29     </update>
30 </mapper>
UserMapper.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 
 7     <!--外部的配置文件-->
 8     <properties resource="jdbc.properties"></properties>
 9 
10     <!--别名-->
11     <typeAliases>
12         <!--<typeAlias type="com.itheima.domain.User" alias="User"/>-->
13         <package name="com.itheima.domain"/>
14     </typeAliases>
15 
16     <!--配置数据源和事务管理-->
17     <environments default="development">
18         <environment id="development">
19             <transactionManager type="JDBC"></transactionManager>
20             <dataSource type="POOLED">
21                 <property name="driver" value="${jdbc.driver}" />
22                 <property name="url" value="${jdbc.url}" />
23                 <property name="username" value="${jdbc.username}" />
24                 <property name="password" value="${jdbc.password}" />
25             </dataSource>
26         </environment>
27 
28         <environment id="test">
29             <transactionManager type="JDBC"></transactionManager>
30             <dataSource type="POOLED">
31                 <property name="driver" value="com.mysql.jdbc.Driver" />
32                 <property name="url" value="jdbc:mysql://localhost:3307/test" />
33                 <property name="username" value="root" />
34                 <property name="password" value="root" />
35             </dataSource>
36         </environment>
37     </environments>
38 
39     <!--映射配置-->
40     <mappers>
41         <!--<mapper resource="com.itheima.mapper.UserMapper.xml"/>-->
42         <mapper resource="com/itheima/mapper/UserMapper.xml"/>
43     </mappers>
44 
45 </configuration>
SqlMapConfig
1 jdbc.driver=com.mysql.jdbc.Driver
2 jdbc.url=jdbc:mysql://localhost:3307/test
3 jdbc.username=root
4 jdbc.password=root
jdbc.properties
  1 package com.itheima;
  2 
  3 import com.itheima.domain.User;
  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 import org.junit.Test;
  9 
 10 import java.io.IOException;
 11 import java.io.InputStream;
 12 import java.util.List;
 13 
 14 public class CRUDTest {
 15 
 16     @Test
 17     public void findAll() throws IOException {
 18 //        String config = "com/itheima/mapper/UserMapper.xml";
 19         String config = "SqlMapConfig.xml";
 20         InputStream resource = Resources.getResourceAsStream(config);
 21         //根据xml配置创建SqlSessionFactory
 22         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
 23         //创建SqlSession
 24         SqlSession sqlSession = sqlSessionFactory.openSession();
 25         //执行语句
 26         List<User> userList = sqlSession.selectList("com.itheima.mapper.UserMapper.findAll");
 27         for (User user : userList) {
 28             System.out.println(user);
 29         }
 30     }
 31 
 32     @Test
 33     public void findById() throws IOException {
 34         //加载配置
 35         String mybatisConfig = "SqlMapConfig.xml";
 36         InputStream mybatisConfigStream = Resources.getResourceAsStream(mybatisConfig);
 37         //创建SqlSessionFactory
 38         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(mybatisConfigStream);
 39         //创建SqlSession
 40         SqlSession sqlSession = sqlSessionFactory.openSession();
 41         //语句
 42         User user = sqlSession.selectOne("com.itheima.mapper.UserMapper.findById", 1);
 43         System.out.println(user);
 44     }
 45 
 46     @Test
 47     public void add() throws IOException {
 48         //准备对象
 49         User user = new User();
 50         user.setUsername("liubei");
 51         user.setPassword("123");
 52 
 53 //        String config = "com/itheima/mapper/UserMapper.xml";
 54         String config = "SqlMapConfig.xml";
 55         InputStream resource = Resources.getResourceAsStream(config);
 56         //根据xml配置创建SqlSessionFactory
 57         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
 58         //创建SqlSession
 59 //        SqlSession sqlSession = sqlSessionFactory.openSession();
 60         SqlSession sqlSession = sqlSessionFactory.openSession(false);
 61         //执行语句
 62         int rows = sqlSession.insert("com.itheima.mapper.UserMapper.add", user);
 63         //提交事务
 64 //        sqlSession.commit();
 65         System.out.println(rows);
 66     }
 67 
 68     @Test
 69     public void delete() throws IOException {
 70         String config = "SqlMapConfig.xml";
 71         InputStream resourceStream = Resources.getResourceAsStream(config);
 72         //创建SqlSessionFactory
 73         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceStream);
 74         //创建SqlSession
 75         SqlSession sqlSession = sqlSessionFactory.openSession();
 76         //删除
 77         int rows = sqlSession.delete("com.itheima.mapper.UserMapper.delete", 10);
 78         //提交事务
 79         sqlSession.commit();
 80         System.out.println(rows);
 81     }
 82 
 83     @Test
 84     public void update() throws IOException {
 85         //准备对象
 86         User user = new User();
 87         user.setId(1);
 88         user.setUsername("root1");
 89         user.setPassword("root");
 90 
 91         //加载核心配置
 92         String mybatisConfig = "SqlMapConfig.xml";
 93         InputStream mybatisConfigStream = Resources.getResourceAsStream(mybatisConfig);
 94         //创建SqlSessionFactory
 95         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(mybatisConfigStream);
 96         //创建SqlSession
 97         SqlSession sqlSession = sqlSessionFactory.openSession();
 98         //语句
 99         int rows = sqlSession.update("com.itheima.mapper.UserMapper.update", user);
100         //手动提交事务
101         sqlSession.commit();
102         System.out.println(rows);
103     }
104 }
CRUDTest

猜你喜欢

转载自www.cnblogs.com/mozq/p/10940313.html
今日推荐