发表于: 2017-12-14 22:55:18
1 687
编辑日报内容...
1.今日完成
编写Mybatis连接数据库:
(1)配置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>
<package name="cn.jnshu.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="cn.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/students?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="cn/jnshu/students/mapper/StudentsMapper.xml"/>
<mapper class="cn.jnshu.students.mapper.StudentsMapper.java"/>
</mappers>
</configuration>
(2)配置文件StudentsMapper.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="StudentsMapper">
<insert id="addStudents" parameterType="Students" >
insert into students (name,qq,school) values (#{name},#{qq},#{school})
</insert>
<delete id="deleteStudentsById" parameterType="int" >
delete from students where id= #{id}
</delete>
<select id="getStudentsById" parameterType="int" resultType="Students">
select * from students where id= #{id}
</select>
<update id="updateStudentsById" parameterType="Students" >
update students set ( name=#{name},qq=#{qq},school=#{school} ) where id=#{id}
</update>
<select id="listStudents" resultType="Students">
select * from students
</select>
</mapper>
(3)Mapper接口文件
package cn.jnshu.students.mapper;
import java.util.List;
import cn.jnshu.pojo.Students;
public interface StudentsMapper {
public void addStudents(Students student) throws Exception;
public void deleteStudentsById(int id) throws Exception;
public Students getStudentsById(int id) throws Exception;
public void updateStudentsById(int id) throws Exception;
public List<Students> listStudents() throws Exception;
}
(4)pojo类(Students类)
package cn.jnshu.pojo;
public class Students {
private int id;
private String name;
private String qq;
private String school;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
}
(5)测试类
package cn.jnshu.students;
import java.io.IOException;
import java.io.InputStream;
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.Test;
import cn.jnshu.pojo.Students;
public class TestMybatis {
// public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
// String resource = "mybatis-config.xml";
// InputStream inputStream = Resources.getResourceAsStream(resource);
// SqlSessionFactory sqlSessionFactory = new
// SqlSessionFactoryBuilder().build(inputStream);
// 根据id查询用户信息,得到一条记录结果
@Test
public void getStudentsById() throws IOException {
// mybatis配置文件
String resource = "mybatis-config.xml";
// 得到配置文件
InputStream inputStream = Resources.getResourceAsStream(resource);
// 创建会话工厂,传入mybatis的配置文件信息
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 通过工厂得到SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 通过SqlSession操作数据
Students Students = sqlSession.selectOne("StudentsMapper.getStudentsById", 1);
System.out.println(Students);
// 释放资源
sqlSession.close();
}
// 根据用户名称模糊查询用户列表
@Test
public void listStudents() throws IOException {
// mybatis配置文件
String resource = "mybatis-config.xml";
// 得到配置文件
InputStream inputStream = Resources.getResourceAsStream(resource);
// 创建会话工厂,传入mybatis的配置文件信息
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 通过工厂得到SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// list中的Students和映射文件中resultType指定的类型一样
List<Students> list = sqlSession.selectOne("StudentsMapper.listStudents");
System.out.println(list);
sqlSession.close();
}
// 添加用户信息
@Test
public void addStudents() throws IOException {
// mybatis配置文件
String resource = "mybatis-config.xml";
// 得到配置文件
InputStream inputStream = Resources.getResourceAsStream(resource);
// 创建会话工厂,传入mybatis的配置文件信息
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 通过工厂得到SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 插入用户对象
Students Students = new Students();
Students.setName("王小军");
Students.setQq("88888888");
Students.setSchool("复旦大学");
sqlSession.insert("StudentsMapper.addStudents", Students);
// 提交事务
sqlSession.commit();
// 获取用户信息主键
System.out.println(Students);
// 关闭会话
sqlSession.close();
}
// 根据id删除 用户信息
@Test
public void deleteStudentsById() throws IOException {
// mybatis配置文件
String resource = "mybatis-config.xml";
// 得到配置文件
InputStream inputStream = Resources.getResourceAsStream(resource);
// 创建会话工厂,传入mybatis的配置文件信息
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 通过工厂得到SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 传入id删除 用户
sqlSession.delete("StudentsMapper.deleteStudentsById", 3);
// 提交事务
sqlSession.commit();
// 关闭会话
sqlSession.close();
}
// 更新用户信息
@Test
public void updateStudentsById() throws IOException {
// mybatis配置文件
String resource = "mybatis-config.xml";
// 得到配置文件
InputStream inputStream = Resources.getResourceAsStream(resource);
// 创建会话工厂,传入mybatis的配置文件信息
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 通过工厂得到SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 更新用户信息
Students Students = new Students();
// 必须设置id
Students.setId(1);
Students.setName("李雷");
Students.setQq("33333333");
Students.setSchool("复旦大学");
sqlSession.update("StudentsMapper.updateStudentsById", Students);
// 提交事务
sqlSession.commit();
// 关闭会话
sqlSession.close();
}
}
2.明日计划
1)学习junit测试
2)编写测试用例
3.遇到的问题
junit测试时,报错如下:
TestMybatis.getStudentsById
getStudentsById(cn.jnshu.students.TestMybatis)
org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in cn/jnshu/students/mapper/StudentsMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.ClassNotFoundException: Cannot find class: cn.jnshu.students.mapper.StudentsMapper.java
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:64)
at cn.jnshu.students.TestMybatis.getStudentsById(TestMybatis.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.ClassNotFoundException: Cannot find class: cn.jnshu.students.mapper.StudentsMapper.java
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:120)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:98)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:78)
... 25 more
Caused by: java.lang.ClassNotFoundException: Cannot find class: cn.jnshu.students.mapper.StudentsMapper.java
at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:200)
at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:89)
at org.apache.ibatis.io.Resources.classForName(Resources.java:261)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:376)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:118)
... 27 more
按照师兄的指点,clean和install之后,还是有如上报错。
4.今日收获
1)了解SSM架构;
2)对Mybatis有了进一步了解
评论