2. 修改pom.xml,添加MyBatis依赖
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.deppon.demo</groupId>
- <artifactId>test05</artifactId>
- <packaging>war</packaging>
- <version>0.0.1-SNAPSHOT</version>
- <name>test05 Maven Webapp</name>
- <url>http://maven.apache.org</url>
- <!-- 属性配置 -->
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.10</version>
- <scope>test</scope>
- </dependency>
- <!-- 添加MyBatis依赖 -->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>3.1.1</version>
- </dependency>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.16</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>1.6.1</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-nop</artifactId>
- <version>1.6.4</version>
- </dependency>
- </dependencies>
- <build>
- <finalName>test05</finalName>
- </build>
- </project>
3. 添加mybatis-config.xml
- <?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>
- <!-- 全局别名设置,在映射文件中只需写别名,而不必写出整个类路径 -->
- <typeAliases>
- <typeAlias type="com.deppon.test05.entity.PersonEntity" alias="PersonEntity"/>
- </typeAliases>
- <environments default="development">
- <environment id="development">
- <transactionManager type="JDBC" />
- <dataSource type="POOLED">
- <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
- <property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=Demo" />
- <property name="username" value="ygy" />
- <property name="password" value="shishi" />
- </dataSource>
- </environment>
- </environments>
- <mappers>
- <mapper resource="com/deppon/test05/mapper/PersonEntityMapper.xml" />
- </mappers>
- </configuration>
PersonEntityMapper.xml
- <?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">
- <mapper namespace="com.deppon.test05.mapper.PersonEntityMapper">
- <!-- 查询所有用户 -->
- <select id="queryAll" resultType="PersonEntity" >
- select * from t_person
- </select>
- <!-- 插入一条记录 -->
- <insert id="insert" parameterType="PersonEntity">
- insert into t_person(id , name) values(#{id} , #{name})
- </insert>
- </mapper>
IPersonEntityDao.java
- package com.deppon.test05.dao;
- import java.util.List;
- import com.deppon.test05.entity.PersonEntity;
- public interface IPersonEntityDao {
- /**
- * 插入一条记录
- * @param person
- */
- public void insert(PersonEntity person);
- /**
- * 查询所有记录
- * @return
- */
- public List<PersonEntity> queryAll();
- }
PersonEntityDao.java
- package com.deppon.test05.dao.impl;
- import java.util.List;
- import org.apache.ibatis.session.SqlSession;
- import com.deppon.test05.dao.IPersonEntityDao;
- import com.deppon.test05.entity.PersonEntity;
- import com.deppon.test05.util.MyBatisUtil;
- public class PersonEntityDao implements IPersonEntityDao {
- public static final String NAMESPACE = "com.deppon.test05.mapper.PersonEntityMapper";
- @Override
- public void insert(PersonEntity person) {
- SqlSession session = MyBatisUtil.getSession();
- session.insert(NAMESPACE + ".insert" , person);
- session.commit();
- session.close();
- }
- @Override
- public List<PersonEntity> queryAll() {
- SqlSession session = MyBatisUtil.getSession();
- List<PersonEntity> personList = session.selectList(NAMESPACE + ".queryAll");
- session.commit();
- session.close();
- return personList;
- }
- }
PersonEntity.java
- package com.deppon.test05.entity;
- public class PersonEntity implements java.io.Serializable {
- private static final long serialVersionUID = -1138245964662330288L;
- private Integer id;
- private String name;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- return "PersonEntity [id=" + id + ", name=" + name + "]";
- }
- }
MyBatisUtil.java
- package com.deppon.test05.util;
- import java.io.IOException;
- 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;
- public class MyBatisUtil {
- private static SqlSessionFactory factory = null;
- private static void initialFactory() {
- String resource = "mybatis-config.xml";
- try {
- InputStream in = Resources.getResourceAsStream(resource);
- factory = new SqlSessionFactoryBuilder().build(in);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static SqlSession getSession() {
- if(factory == null) {
- initialFactory();
- }
- return factory.openSession();
- }
- }
测试程序:
- package com.deppon.test05.dao;
- import java.util.List;
- import org.junit.Before;
- import org.junit.Test;
- import com.deppon.test05.dao.impl.PersonEntityDao;
- import com.deppon.test05.entity.PersonEntity;
- public class PersonEntityDaoTest {
- private IPersonEntityDao personEntityDao;
- @Before
- public void before() {
- personEntityDao = new PersonEntityDao();
- }
- @Test
- public void testQueryAll() {
- List<PersonEntity> personList = personEntityDao.queryAll();
- for(PersonEntity each : personList) {
- System.out.println(each);
- }
- }
- @Test
- public void testInsert() {
- PersonEntity person = new PersonEntity();
- person.setId(200);
- person.setName("乔巴");
- personEntityDao.insert(person);
- }
- }
项目结构如下图所示:
注意:记得那两条命令哦,亲(前面的博客有)