参考动力节点网课
该程序功能:将学生信息导入对应的数据库
第一步:导入包
Mybatis的依赖包,核心包,以及mysql的连接驱动,junit核心包,junit的依赖包 hamcrest-core-1.3.jar
第二步:定义学生类,表,Dao接口
package rodisland;
//为了能使用反射机制创建对象,必须给出无参数的构造函数
public class Student
{
private String name;
private int age;
private int num;
public Student()
{
name=null;
age=0;
num=0;
}
public Student(String name,int age,int num)
{
this.name=name;
this.age=age;
this.num=num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String toString()
{
return("name:"+name+" age:"+age+" num:"+num);
}
}
并在同一个包中定义Dao接口
package rodisland;
public interface Dao
{
void insertStudent(Student student);
}
在数据库中创建相应的表格:
create table Student(
name Varchar (10),
age int (2),
num int (5)
);
第三步:完成配置文件以及映射文件
映射文件:以插入功能为例
<?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="test">
<insert id="insertStu">
insert into Student(name,age,num)
values(#{name},#{age},#{num})
</insert>
</mapper>
配置文件:
<?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>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<environments default="mysqlEM">
<environment id="mysqlEM">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3366/rodisland"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="rodisland/mapper.xml" />
</mappers>
</configuration>
第四步:实现学生类Dao接口
package rodisland;
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 StudentDaoImp implements Dao
{
private SqlSession session;
@Override
public void insertStudent(Student student)
{
try
{
//1.读取主配置文件
InputStream inputstream=Resources.getResourceAsStream("rodisland/mybatis.xml");
//2.创建SqlSessionFactory对象
SqlSessionFactory sqlSessionfactory=new SqlSessionFactoryBuilder().build(inputstream);
//3.创建SqlSession对象
session=sqlSessionfactory.openSession();
//4.操作
session.insert("insertStu",student);
//5.提交事务
session.commit();
}catch(IOException e)
{
e.printStackTrace();
}finally
{
//6.关闭session
if(session!=null)
session.close();
}
}
}
第五步:测试类创建
package test;
import org.junit.Test;
import rodisland.Dao;
import rodisland.Student;
import rodisland.StudentDaoImp;
public class Mytest
{
@Test
public void Test1()
{
Dao dao=new StudentDaoImp();
Student student=new Student("霜叶",16,7809);
dao.insertStudent(student);
}
}
第六步:完成log4j配置文件,将运行结果输出到控制台
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}]%c %L %m%n
log4j.rootLogger=debug,console
测试结果(控制台输出)
异常分析
1.进行了第一次测试,出现以下报错
Could not find resource /firstBatis/src/mapper.xml
查到的原因:编译器不会编译src下的xml,需要移出src,并addpath
但是Eclipse可以自动加载src下的xml文件,故不需要上述操作,正确操作
<mapper resource="rodisland/mapper.xml" />
InputStream inputstream=Resources.getResourceAsStream("rodisland/mybatis.xml");
包名+文件名即可,不需要全部路径
2.第二次测试报错,sql驱动无法加载
<property name="driver" value="com.mysql.jdbc.Driver.class"/>
不需要文件名,删除
<property name="driver" value="com.mysql.jdbc.Driver"/>
即可
3.无法打开log日志,控制台无任何信息输出
在环境配置加上
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
要注意标签的位置,不可颠倒,否则会报错