【需求说明】
1.搭建MyBatis面向Mapper接口的JavaWeb开发环境。
【资源准备】
1.搭建本次开发环境需要使用的jar包如下,欢迎点击下载:
链接:https://pan.baidu.com/s/1WsobyRJHy_TGhkDzsO9-jA&shfl=sharepset
提取码:c8e3
项目源码在本文末附录。
【开发平台】
OS:Windows10 IDE:Eclipse Jee 2019-06 DB:MySQL
WebServer:Apache Tomcat 9.0
【项目部署】
1.在Eclipse Jee 2019-06中新建一个Dynamic Web Project ,命名为:MyBatisOInterf。文件部署情况如下图所示:
【注意】1.com.dao包下的接口与SQL映射文件必须同名。2.MyBatis配置文件mybatis-config.xml和log4j属性文件存放在src文件根目录
2.导入jar包:将刚才下载好的jar包导入项目文件夹WEB-INF下的lib文件夹下(其他导入方式也行),如下图所示:
3.建立数据库和数据表:我构建的数据库名为test,表名为student,如下图所示:
4.创建持久化类(com.mybatis.po包下)名为Student的类,代码如下:
package com.mybatis.po;
/**
* @author HackerAC
*/
public class Student {
private int stu_Num;
private String stu_Name,stu_Class,stu_Sex,academy,profession;
public Student() {}
public Student(int stu_Num, String stu_Name, String stu_Sex, String academy, String profession, String stu_Class) {
this.stu_Num = stu_Num;
this.stu_Name = stu_Name;
this.stu_Class = stu_Class;
this.stu_Sex = stu_Sex;
this.academy = academy;
this.profession = profession;
}
public int getStu_Num() {
return stu_Num;
}
public void setStu_Num(int stu_Num) {
this.stu_Num = stu_Num;
}
public String getStu_name() {
return stu_Name;
}
public void setStu_name(String stu_name) {
this.stu_Name = stu_name;
}
public String getStu_class() {
return stu_Class;
}
public void setStu_class(String stu_class) {
this.stu_Class = stu_class;
}
public String getStu_Sex() {
return stu_Sex;
}
public void setStu_Sex(String stu_Sex) {
this.stu_Sex = stu_Sex;
}
public String getAcademy() {
return academy;
}
public void setAcademy(String academy) {
this.academy = academy;
}
public String getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}
@Override
public String toString() {
return "Student [stu_Num=" + stu_Num + ", stu_name=" + stu_Name + ", stu_class=" + stu_Class + ", stu_Sex="
+ stu_Sex + ", academy=" + academy + ", profession=" + profession + "]";
}
}
【注意】该类的属性名必须和数据表字段名一致。
5.创建MyBatis的配置文件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">
<!--@auther:HackerAC-->
<configuration>
<settings>
<setting name="logImpl" value="LOG4J" />
<setting name="defaultExecutorType" value="BATCH" />
</settings>
<typeAliases>
<package name="com.mybatis.po"/>
</typeAliases>
<environments default="">
<environment id="">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8&serverTimezone=GMT%2B8" />
<property name="username" value="root" />
<property name="password" value="xlt123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/dao/StudentDAO.xml"/>
</mappers>
</configuration>
6.配置自定义的log4j.properties,代码如下:
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%-5p] [%d{yyyy-MM-dd HH:mm:ss}] [%t] %c %L %m%n
log4j.rootLogger=DEBUG,console
7.创建接口:
/**
* @author HackerAC
*/
package com.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.mybatis.po.Student;
public interface StudentDAO {
//批量插入
public void InserStuList(@Param("stulist") List<Student> stulist);
//查询所有学生信息
public List<Student> SelectAll();
}
8.创建SQL映射文件:
<?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.dao.StudentDAO">
<sql id="mysql">
(stu_Num,stu_Name,stu_Sex,academy,profession,stu_Class)
</sql>
<!-- 批量插入 -->
<insert id="InserStuList">
insert into student <include refid="mysql"/> values
<trim suffixOverrides=",">
<foreach collection="stulist" item="item" >
( #{item.stu_Num}, #{item.stu_Name}, #{item.stu_Sex}, #{item.academy}, #{item.profession}, #{item.stu_Class}),
</foreach>
</trim>
</insert>
<!-- 查询全部 -->
<select id="SelectAll" resultType="Student">
select * from student
</select>
</mapper>
9.创建测试类:
package com.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
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.After;
import org.junit.Before;
import org.junit.Test;
import com.dao.StudentDAO;
import com.mybatis.po.Student;
/**
* @author HackerAC
*/
public class test {
private SqlSession session = null;
private SqlSessionFactory build;
private List<Student> stuList=new ArrayList<Student>();
@Before
public void init() {
InputStream is;
try {
is = Resources.getResourceAsStream("mybatis-config.xml");
build = new SqlSessionFactoryBuilder().build(is);
session = build.openSession();
} catch (IOException e) {
e.printStackTrace();
}
}
@After
public void destory() {
if (session != null) {
session.close();
}
}
// 新增数据
@Test
public void testInserstulist() {
Student stu1=new Student(20172000,"学生1","男","计算机学院","软件工程","3");
Student stu2=new Student(20172001,"学生2","女","计算机学院","大数据","1");
Student stu3=new Student(20172002,"学生3","男","计算机学院","人工智能","4");
Student stu4=new Student(20172003,"学生4","女","计算机学院","物联网","5");
stuList.add(stu1);
stuList.add(stu2);
stuList.add(stu3);
stuList.add(stu4);
session.getMapper(StudentDAO.class).InserStuList(stuList);
session.commit();
}
@Test
public void testselectAll() {
stuList=session.getMapper(StudentDAO.class).SelectAll();
for(Student stu : stuList) {
System.out.println(stu);
}
}
}
【运行结果】
添加学生:
添加成功:
查询学生信息(同样使用Junit4运行testselectAll()方法)结果为:
经上述测试结果可知环境搭建成功。至此,Mybatis面向接口编程的JavaWeb开发环境已经搭建完成。
【附录】
本文项目源码下载:
链接:https://pan.baidu.com/s/1P8fyMO3l1JW9q6te_Mpfpw&shfl=sharepset
提取码:jhs0