mybatis环境搭建步骤(含配置文件代码)
1.创建web项目
2.将所需要的jar包放在项目内,并且build-path
3.创建资源文件夹resources
4.在资源文件夹中创建xml文件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 alias="Dept" type="org.entity.Dept"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/schooldb"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 加载的映射文件 -->
<mapper resource="org/dao/IDeptDao.xml"/>
</mappers>
</configuration>
5.创建entity包,根据数据库表创建实体类,一个表对应一个实体类,表中的字段对应实体类中的属性
6.创建dao包,在dao包中创建接口(在接口写方法,增删改查)
public int addDept(Dept dept);
public List<Dept> findDeptAll();
7.在dao层中编写mapper.xml文件,一般mapper文件和接口文件名相同
8.IDeptDao.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="org.dao.IDeptDao">
<!-- 查询部门的数量 -->
<select id="findCountDept" resultType="int">
select count(*) from dept
</select>
<!-- 添加信息 -->
<insert id="addDept" parameterType="org.entity.Dept">
insert into dept(did,dname) values(#{did},#{dname});
</insert>
<!-- 修改信息 -->
<update id="updateDept" parameterType="org.entity.Dept">
update dept set dname = #{dname} where did = #{did}
</update>
<!-- 删除 -->
<delete id="delDept" parameterType="org.entity.Dept" >
delete from dept where did = #{did}
</delete>
<!-- 查询全部 -->
<select id="findDeptAll" resultType="org.entity.Dept">
select * from dept;
</select>
<!-- 根据编号查询信息 -->
<select id="findDeptById" resultType="org.entity.Dept"
parameterType="int">
select * from dept where did = #{did}
</select>
</mapper>
9.编写测试类,代码如下:
package org.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Scanner;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.dao.IDeptDao;
import org.entity.Dept;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestMain {
//单元测试
String path = "mybatis-config.xml";
SqlSession session = null;
InputStream in =null;
@Before
public void before(){
try {
in = Resources.getResourceAsStream(path);
session = new SqlSessionFactoryBuilder().build(in).openSession();
} catch (IOException e) {
e.printStackTrace();
}
}
//查询部门数量
@Test
public void testCount() {
// 加载xml文件
int count = session.selectOne("org.dao.IDeptDao.findCountDept");
System.out.println("一共有:" + count + "个部门");
}
//添加信息
@Test
public void addDept(){
Dept dept = new Dept();
dept.setDid(6);
dept.setDname("运维部");
int rel = session.insert("org.dao.IDeptDao.addDept",dept);
//提交(增删改)
session.commit();
if(rel>0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
}
//查询全部
@Test
public void findAllDept(){
List<Dept> dList = session.getMapper(IDeptDao.class).findDeptAll();
for (Dept dept : dList) {
System.out.println("编号:"+dept.getDid()+",名称:"+dept.getDname());
}
}
//根据编号查询
public Dept findDeptById(int did){
Dept dept = session.getMapper(IDeptDao.class).findDeptById(did);
return dept;
}
//修改
@Test
public void updateDel(){
findAllDept();
System.out.println("请输入用户编号:");
Scanner sc = new Scanner(System.in);
int did = sc.nextInt();
//根据编号查询
Dept dept = findDeptById(did);
System.out.println("需要修改的部门信息:");
System.out.println("编号:"+dept.getDid()+",名称:"+dept.getDname());
System.out.println("请输入新的部门名称:");
dept.setDname(sc.next());
//调用修改的方法
int rel = session.getMapper(IDeptDao.class).updateDept(dept);
//提交
session.commit();
if(rel>0){
System.out.println("修改成共");
}else{
System.out.println("修改失败");
}
}
//删除
@Test
public void delDept(){
findAllDept();
System.out.println("请输入需要删除的编号:");
Scanner sc = new Scanner(System.in);
Dept dept = session
.getMapper(IDeptDao.class)
.findDeptById(sc.nextInt());
//调用删除
int rel = session.getMapper(IDeptDao.class).delDept(dept);
session.commit();
if(rel>0){
System.out.println("删除成功");
findAllDept();
}
}
//最后执行
@After
public void after(){
if(session!=null){
session.close();
}
}
}
往期精彩
点分享
点点赞
点在看