配置文件缺失
- 通过Maven进行重编译
- Rebuild Project
- 粘贴复制
插入和删除
TestMybatis
package com.sdut;
import com.sdut.entity.Student;
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.Test;
import java.io.IOException;
import java.io.InputStream;
public class TestMybatis {
//测试方法
@Test
public void testInsert() throws IOException {
String config = "mybatis.xml";
InputStream in = Resources.getResourceAsStream(config);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
SqlSession sqlSession = factory.openSession();
String sqlId = "com.sdut.dao.StudentDao.insertStudent";
Student student = new Student();
student.setId(1003);
student.setName("王五");
student.setEmail("110@qq.com");
student.setAge(20);
int result = sqlSession.insert(sqlId, student);
System.out.println(result);
//mybatis默认不自动提交事务,insert, delete, update后要手动提交事务
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteStudentById() throws IOException {
String config = "mybatis.xml";
InputStream in = Resources.getResourceAsStream(config);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
SqlSession sqlSession = factory.openSession();
String sqlId = "com.sdut.dao.StudentDao.deleteStudentById";
sqlSession.delete(sqlId, 1003);
sqlSession.commit();
sqlSession.close();
}
}
StudentDao
package com.sdut.dao;
import com.sdut.entity.Student;
import java.util.List;
//一个接口,操作student表
public interface StudentDao {
//查询所有数据
public List<Student> selectAllStudents();
//插入一条记录
public int insertStudent(Student student);
//删除一条记录
public int deleteStudentById(int id);
}
StudentDao.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.sdut.dao.StudentDao">
<select id="selectAllStudents" resultType="com.sdut.entity.Student">
select id, name, email, age from student order by id
</select>
<insert id="insertStudent">
insert into student values(#{id}, #{name}, #{email}, #{age})
</insert>
<delete id="deleteStudentById">
delete from student where id=#{id}
</delete>
</mapper>
运行结果:
配置日志文件
mybatis.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>
<!-- 控制mybatis全局行为的 -->
<settings>
<!-- 设置mybatis输出日志 -->
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<environments default="mydev">
<environment id="mydev">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssm"/>
<property name="username" value="root"/>
<property name="password" value="010203"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/sdut/dao/StudentDao.xml"/>
</mappers>
</configuration>