(八)- 构建MyBatis项目
最后更新于:2022-04-01 20:44:55
在这里,写一下,怎么使用Maven构建MyBatis项目。
### 1. 新建一个Web项目
可以参考前面的博客
### 2. 修改pom.xml,添加MyBatis依赖
~~~
4.0.0
com.deppon.demo
test05
war
0.0.1-SNAPSHOT
test05 Maven Webapp
http://maven.apache.org
UTF-8
junit
junit
4.10
test
org.mybatis
mybatis
3.1.1
log4j
log4j
1.2.16
org.slf4j
slf4j-api
1.6.1
org.slf4j
slf4j-nop
1.6.4
test05
~~~
### 3. 添加mybatis-config.xml
~~~
~~~
PersonEntityMapper.xml
~~~
insert into t_person(id , name) values(#{id} , #{name})
~~~
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 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 queryAll() {
SqlSession session = MyBatisUtil.getSession();
List 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 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);
}
}
~~~
项目结构如下图所示:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-09-06_57ce64fcd9ec4.png)
注意:记得那两条命令哦,亲![大笑](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-19_569e21abc5518.gif)
(前面的博客有)
结果:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-09-06_57ce64fd03b4b.png)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- [Maven学习(一)- 环境搭建](http://blog.csdn.net/jolingogo/article/details/8775046)
- [Maven学习(二)- 安装m2eclipse插件 ](http://blog.csdn.net/jolingogo/article/details/8796410)
- [Maven学习(三)- 使用Maven构建Web项目](http://blog.csdn.net/jolingogo/article/details/8796726)
- [Maven学习(四)- 使用Maven构建Web项目-测试](http://blog.csdn.net/jolingogo/article/details/8797153)
- [Maven学习(五)- 使用Maven构建Struts2项目](http://blog.csdn.net/jolingogo/article/details/8798052)
- [Maven学习(六)- 构建Hibernate项目](http://blog.csdn.net/jolingogo/article/details/8798684)
- [Maven学习(七)- 构建Spring项目](http://blog.csdn.net/jolingogo/article/details/8799307)
- [Maven学习(八)- 构建MyBatis项目](http://blog.csdn.net/jolingogo/article/details/8801158)
- [Maven学习(九)- 构建SSH项目](http://blog.csdn.net/jolingogo/article/details/8811817)
- [Maven学习(十) - 阶段小结
](http://blog.csdn.net/jolingogo/article/details/8821375)
- [专栏:Maven学习之旅](http://blog.csdn.net/column/details/yuguiyang-maven.html)
';