Mybatis学习1

Mybatis学习框架

1、引入相应jar包(或相关依赖)

部分依赖

 1 <dependencies>
 2       <dependency>
 3           <groupId>mysql</groupId>
 4           <artifactId>mysql-connector-java</artifactId>
 5           <version>5.1.7</version>
 6           <type>pom.lastUpdated</type>
 7       </dependency>
 8       <dependency>
 9           <groupId>asm</groupId>
10           <artifactId>asm</artifactId>
11           <version>3.3.1</version>
12       </dependency>
13       <dependency>
14           <groupId>cglib</groupId>
15           <artifactId>cglib-nodep</artifactId>
16           <version>3.2.6</version>
17       </dependency>
18       <dependency>
19           <groupId>commons-logging</groupId>
20           <artifactId>commons-logging</artifactId>
21           <version>1.1.1</version>
22       </dependency>
23       <dependency>
24           <groupId>junit</groupId>
25           <artifactId>junit</artifactId>
26           <version>4.12</version>
27       </dependency>
28       <dependency>
29           <groupId>org.mybatis</groupId>
30           <artifactId>mybatis</artifactId>
31           <version>3.4.6</version>
32       </dependency>
33 
34       <dependency>
35           <groupId>log4j</groupId>
36           <artifactId>log4j</artifactId>
37           <version>1.2.12</version>
38       </dependency>
39   </dependencies>

2.配置相关文件

核心配置文件sqlMapperConfig.xml

包括:数据源配置和引入pojo.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     <!-- 和spring整合后 environments配置将废除 -->
 7     <environments default="development">
 8         <environment id="development">
 9             <!-- 使用jdbc事务管理 -->
10             <transactionManager type="JDBC" />
11             <!-- 数据库连接池 -->
12             <dataSource type="POOLED">
13                 <property name="driver" value="com.mysql.jdbc.Driver" />
14                 <property name="url"
15                     value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" />
16                 <property name="username" value="root" />
17                 <property name="password" value="123456" />
18             </dataSource>
19         </environment>
20     </environments>
21     
22     <!-- 加载映射文件 -->
23     <mappers>
24         <mapper resource="mybatis/user.xml"/>
25     </mappers>
26 </configuration>

日志文件log4j.properties

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

3.创建实体和mapper映射文件

User.java

 1 package pojo;
 2 
 3 import java.util.Date;
 4 
 5 public class User {
 6 
 7     private Integer id;
 8     private String username;// 鐢ㄦ埛濮撳悕
 9     private String sex;// 鎬у埆
10     private Date birthday;// 鐢熸棩
11     private String address;// 鍦板潃
12     private String uuid2;
13 
14     public String getUuid2() {
15         return uuid2;
16     }
17 
18     public void setUuid2(String uuid2) {
19         this.uuid2 = uuid2;
20     }
21 
22     public Integer getId() {
23         return id;
24     }
25 
26     public void setId(Integer id) {
27         this.id = id;
28     }
29 
30     public String getUsername() {
31         return username;
32     }
33 
34     public void setUsername(String username) {
35         this.username = username;
36     }
37 
38     public String getSex() {
39         return sex;
40     }
41 
42     public void setSex(String sex) {
43         this.sex = sex;
44     }
45 
46     public Date getBirthday() {
47         return birthday;
48     }
49 
50     public void setBirthday(Date birthday) {
51         this.birthday = birthday;
52     }
53 
54     public String getAddress() {
55         return address;
56     }
57 
58     public void setAddress(String address) {
59         this.address = address;
60     }
61 
62     @Override
63     public String toString() {
64         return "User [id=" + id + ", username=" + username + ", sex=" + sex + ", birthday=" + birthday + ", address="
65                 + address + ", uuid2=" + uuid2 + "]";
66     }
67     
68     
69 }

user.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 <!-- namespace命名空间,隔离sql文件 -->
 6 <!-- #{}占位符相当于jdbc的? -->
 7 <!-- ${value} 字符串拼接 -->
 8 <mapper namespace="test">
 9     <select id="getUserById" parameterType="int" resultType="pojo.User">
10         select id, username,birthday,sex,address from user where id = #{id}
11     </select>
12     <select id="getUserByName" parameterType="String" resultType="pojo.User">
13         select id, username,birthday,sex,address from user where username like '%${value}%'
14     </select>
15     <insert id="insertUser" parameterType="pojo.User" useGeneratedKeys="true" keyProperty="id">
16     <!-- 主键返回 -->
17         <!-- <selectKey keyProperty="id" resultType="int" order="AFTER">
18             SELECT LAST_INSERT_ID()
19         </selectKey> -->
20          insert into user ( username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address});
21     </insert>
22     
23     <insert id="insertUserUUID" parameterType="pojo.User">
24     <!-- 主键返回 -->
25         <selectKey keyProperty="uuid2" resultType="String" order="BEFORE">
26             SELECT UUID();
27         </selectKey> 
28          insert into user ( username,birthday,sex,address,uuid2) values(#{username},#{birthday},#{sex},#{address},#{uuid2});
29     </insert>
30 </mapper>

测试文件

 1 package test;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.util.List;
 6 
 7 import org.apache.ibatis.io.Resources;
 8 import org.apache.ibatis.session.SqlSession;
 9 import org.apache.ibatis.session.SqlSessionFactory;
10 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
11 
12 import pojo.User;
13 import util.SqlSessionFactoryUtil;
14 
15 public class Test {
16     
17     @org.junit.Test
18     public void test1() throws IOException {
19         //1.创建SqlSessionFactoryBuilder对象
20         SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
21         //2.配置核心文件输入流
22         InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapperConfig.xml");
23         //通过输入流创建SqlSessionFactory
24         SqlSessionFactory build = ssfb.build(resourceAsStream);
25         //创建SqlSession对象
26         SqlSession openSession = build.openSession();
27         //执行查询
28         User user =  openSession.selectOne("test.getUserById", 1);
29         System.out.println(user);
30         openSession.close();
31     }
32     
33     @org.junit.Test
34     public void test2() throws IOException {
35         SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();
36         SqlSession openSession = sqlSessionFactory.openSession();
37         List<User> list = openSession.selectList("test.getUserByName", "%张%");
38         
39         for (User user : list) {
40             System.out.println(user);
41         }
42     
43     }
44     @org.junit.Test
45     public void test3() throws IOException {
46         SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();
47         SqlSession openSession = sqlSessionFactory.openSession(true);
48         User user = new User();
49         user.setAddress("武汉");
50         user.setUsername("张飞3");
51         user.setSex("男");
52         openSession.insert("test.insertUserUUID", user);
53         System.out.println(user);
54         //提交数据
55     
56     }
57 }

猜你喜欢

转载自www.cnblogs.com/jinyu-helloword/p/10663200.html