maven学习5 构建MyBatis项目

2. 修改pom.xml,添加MyBatis依赖

[html] view plain copy
 
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   <groupId>com.deppon.demo</groupId>  
  5.   <artifactId>test05</artifactId>  
  6.   <packaging>war</packaging>  
  7.   <version>0.0.1-SNAPSHOT</version>  
  8.   <name>test05 Maven Webapp</name>  
  9.   <url>http://maven.apache.org</url>  
  10.     
  11.   <!-- 属性配置 -->  
  12.   <properties>  
  13.       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  14.   </properties>  
  15.     
  16.   <dependencies>  
  17.     <dependency>  
  18.       <groupId>junit</groupId>  
  19.       <artifactId>junit</artifactId>  
  20.       <version>4.10</version>  
  21.       <scope>test</scope>  
  22.     </dependency>  
  23.       
  24.     <!-- 添加MyBatis依赖 -->  
  25.     <dependency>  
  26.         <groupId>org.mybatis</groupId>  
  27.         <artifactId>mybatis</artifactId>  
  28.         <version>3.1.1</version>  
  29.     </dependency>  
  30.       
  31.     <dependency>  
  32.         <groupId>log4j</groupId>  
  33.         <artifactId>log4j</artifactId>  
  34.         <version>1.2.16</version>  
  35.     </dependency>  
  36.       
  37.     <dependency>  
  38.       <groupId>org.slf4j</groupId>  
  39.       <artifactId>slf4j-api</artifactId>  
  40.       <version>1.6.1</version>  
  41.     </dependency>  
  42.       
  43.     <dependency>  
  44.         <groupId>org.slf4j</groupId>  
  45.         <artifactId>slf4j-nop</artifactId>  
  46.         <version>1.6.4</version>  
  47.     </dependency>  
  48.       
  49.   </dependencies>  
  50.     
  51.   <build>  
  52.     <finalName>test05</finalName>  
  53.   </build>  
  54. </project>  

3. 添加mybatis-config.xml

[html] view plain copy
 
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2.   
  3. <!DOCTYPE configuration  
  4.   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
  5.   "http://mybatis.org/dtd/mybatis-3-config.dtd">  
  6.       
  7. <configuration>  
  8.     <!-- 全局别名设置,在映射文件中只需写别名,而不必写出整个类路径  -->  
  9.     <typeAliases>    
  10.          <typeAlias type="com.deppon.test05.entity.PersonEntity" alias="PersonEntity"/>  
  11.     </typeAliases>   
  12.       
  13.     <environments default="development">  
  14.         <environment id="development">  
  15.             <transactionManager type="JDBC" />  
  16.             <dataSource type="POOLED">  
  17.                 <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />  
  18.                 <property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=Demo" />  
  19.                 <property name="username" value="ygy" />  
  20.                 <property name="password" value="shishi" />  
  21.             </dataSource>  
  22.         </environment>  
  23.     </environments>  
  24.   
  25.     <mappers>  
  26.         <mapper resource="com/deppon/test05/mapper/PersonEntityMapper.xml" />  
  27.     </mappers>  
  28.   
  29. </configuration>  

PersonEntityMapper.xml

[html] view plain copy
 
  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. <mapper namespace="com.deppon.test05.mapper.PersonEntityMapper">  
  7.   
  8.     <!-- 查询所有用户 -->  
  9.     <select id="queryAll" resultType="PersonEntity" >  
  10.         select * from t_person  
  11.     </select>  
  12.       
  13.     <!-- 插入一条记录 -->  
  14.     <insert id="insert" parameterType="PersonEntity">  
  15.         insert into t_person(id , name) values(#{id} , #{name})  
  16.     </insert>  
  17.       
  18. </mapper>   


IPersonEntityDao.java

[java] view plain copy
 
  1. package com.deppon.test05.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.deppon.test05.entity.PersonEntity;  
  6.   
  7. public interface IPersonEntityDao {  
  8.     /** 
  9.      * 插入一条记录 
  10.      * @param person 
  11.      */  
  12.     public void insert(PersonEntity person);  
  13.       
  14.     /** 
  15.      * 查询所有记录 
  16.      * @return 
  17.      */  
  18.     public List<PersonEntity> queryAll();  
  19. }  


PersonEntityDao.java

[java] view plain copy
 
  1. package com.deppon.test05.dao.impl;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.apache.ibatis.session.SqlSession;  
  6.   
  7. import com.deppon.test05.dao.IPersonEntityDao;  
  8. import com.deppon.test05.entity.PersonEntity;  
  9. import com.deppon.test05.util.MyBatisUtil;  
  10.   
  11. public class PersonEntityDao implements IPersonEntityDao {  
  12.     public static final String NAMESPACE = "com.deppon.test05.mapper.PersonEntityMapper";  
  13.       
  14.     @Override  
  15.     public void insert(PersonEntity person) {  
  16.         SqlSession session = MyBatisUtil.getSession();  
  17.         session.insert(NAMESPACE + ".insert" , person);  
  18.           
  19.         session.commit();  
  20.         session.close();  
  21.     }  
  22.   
  23.     @Override  
  24.     public List<PersonEntity> queryAll() {  
  25.         SqlSession session = MyBatisUtil.getSession();  
  26.         List<PersonEntity> personList = session.selectList(NAMESPACE + ".queryAll");  
  27.           
  28.           
  29.         session.commit();  
  30.         session.close();  
  31.           
  32.         return personList;  
  33.     }  
  34.   
  35. }  


PersonEntity.java

[java] view plain copy
 
  1. package com.deppon.test05.entity;  
  2.   
  3. public class PersonEntity implements java.io.Serializable {  
  4.     private static final long serialVersionUID = -1138245964662330288L;  
  5.   
  6.     private Integer id;  
  7.     private String name;  
  8.   
  9.     public Integer getId() {  
  10.         return id;  
  11.     }  
  12.   
  13.     public void setId(Integer id) {  
  14.         this.id = id;  
  15.     }  
  16.   
  17.     public String getName() {  
  18.         return name;  
  19.     }  
  20.   
  21.     public void setName(String name) {  
  22.         this.name = name;  
  23.     }  
  24.   
  25.     @Override  
  26.     public String toString() {  
  27.         return "PersonEntity [id=" + id + ", name=" + name + "]";  
  28.     }  
  29.       
  30. }  


MyBatisUtil.java

[java] view plain copy
 
  1. package com.deppon.test05.util;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5.   
  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. public class MyBatisUtil {  
  12.     private static SqlSessionFactory factory = null;  
  13.       
  14.     private static void initialFactory() {  
  15.         String resource = "mybatis-config.xml";  
  16.         try {  
  17.             InputStream in = Resources.getResourceAsStream(resource);  
  18.             factory = new SqlSessionFactoryBuilder().build(in);  
  19.         } catch (IOException e) {  
  20.             e.printStackTrace();  
  21.         }  
  22.           
  23.     }  
  24.       
  25.     public static SqlSession getSession() {  
  26.         if(factory == null) {  
  27.             initialFactory();  
  28.         }  
  29.           
  30.         return factory.openSession();  
  31.     }  
  32. }  

测试程序:

[java] view plain copy
 
  1. package com.deppon.test05.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.junit.Before;  
  6. import org.junit.Test;  
  7.   
  8. import com.deppon.test05.dao.impl.PersonEntityDao;  
  9. import com.deppon.test05.entity.PersonEntity;  
  10.   
  11. public class PersonEntityDaoTest {  
  12.     private IPersonEntityDao personEntityDao;  
  13.       
  14.     @Before  
  15.     public void before() {  
  16.         personEntityDao = new PersonEntityDao();  
  17.     }  
  18.       
  19.     @Test  
  20.     public void testQueryAll() {  
  21.         List<PersonEntity> personList = personEntityDao.queryAll();  
  22.           
  23.         for(PersonEntity each : personList) {  
  24.             System.out.println(each);  
  25.         }  
  26.     }  
  27.       
  28.     @Test  
  29.     public void testInsert() {  
  30.         PersonEntity person = new PersonEntity();  
  31.         person.setId(200);  
  32.         person.setName("乔巴");  
  33.           
  34.         personEntityDao.insert(person);  
  35.     }  
  36. }  


项目结构如下图所示:

注意:记得那两条命令哦,亲大笑(前面的博客有)

原文地址:https://www.cnblogs.com/liufei1983/p/7446118.html