使用mapper代理開發的規範java
總結:mysql
給出一個實際開發的案例 :sql
maven文件數據庫
<dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.7</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.43</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- 引入dbcp鏈接池 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.8</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.8</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.7</version> </dependency> <dependency> <groupId>asm</groupId> <artifactId>asm</artifactId> <version>3.3</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-ehcache</artifactId> <version>1.0.0</version> </dependency> </dependencies>
mybatis全局配置文件 sqlMapConfig.xml :apache
<?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> <!-- 1 加載配置文件 --> <properties resource="db.properties"></properties> <!-- 2 對鏈接池 和 事物的配置 (這裏必須粘貼以前的 本身寫的如出一轍都報錯) --> <environments default="development"> <environment id="development"> <!-- 使用jdbc事物管理,事物控制由mybatis管理 --> <transactionManager type="JDBC" /> <!-- 數據庫鏈接池 --> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.uername}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> </environments> <!-- 3 在sqlMapconfig中加載咱們的配置文件 --> <mappers> <mapper resource="com/shi/mapper/UserMapper.xml"/> </mappers> </configuration>
UserMapper.xml文件:api
<?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:必須是和mapper.java的相對路徑 --> <mapper namespace="com.shi.mapper.UserMapper"> <select id="selectUserById" parameterType="Integer" resultType="com.shi.pojo.User"> select * from User WHERE id=#{id} </select> </mapper>
UserMapper.java文件session
package com.shi.mapper; import com.shi.pojo.User; public interface UserMapper { /** * 操做user的mapper的接口 * @param id * @return * @throws Exception */ public User selectUserById(Integer id)throws Exception; }
測試文件 test.javamybatis
package com.shi.mapper; import static org.junit.Assert.*; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Before; import org.junit.Test; import com.shi.pojo.User; public class UserMapperTest { private SqlSessionFactory sqlSessionFactory; /** * 在測試以前 咱們要獲得 會話工廠 * @throws Exception */ @Before public void setUp() throws Exception{ //1 先獲得配置文件 InputStream inputStream=Resources.getResourceAsStream("sqlMapConfig.xml"); //2 根據配置文件呢 建立咱們的會話工廠 sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); } @Test public void testSelectUserById() throws Exception { //3 打開一個會話 SqlSession sqlSession=sqlSessionFactory.openSession(); //4 建立UserMapper接口的實現類,mybatis自動生成代理對象,自動生成其實現類 UserMapper userMapper=sqlSession.getMapper(UserMapper.class); //5 調用方法 執行 User user=userMapper.selectUserById(2); //6 關閉會話 sqlSession.close(); System.out.println(user); } }
pojo對象 User.javaapp
package com.shi.pojo; public class User { private Integer id; private String username; private String birthday; private String sex; private String address; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", birthday=" + birthday + ", sex=" + sex + ", address=" + address + "]"; } }