老师看一下
package com.imooc.utils;
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 java.io.IOException;
import java.io.Reader;
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory = null;
static{
Reader reader = null;
try {
reader = Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}catch(Exception e){
e.printStackTrace();
throw new ExceptionInInitializerError();
}
}
public static SqlSession openSession(){
return sqlSessionFactory.openSession(false);
}
public static void closeSession(SqlSession sqlSession){
if (sqlSession != null) {
sqlSession.close();
}
}
}<?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="mapUnderscoreToCamelCase" value="true"/> </settings> <environments default="stu"> <environment id="stu"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/imooc?useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="ccc980916"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mappers/student.xml"/> </mappers> </configuration>
package com.imooc.entity;
public class Student {
private Integer id;
private Integer regNo;
private String name;
private String sex;
private Integer age;
private String grade;
private String major;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getregNo() {
return regNo;
}
public void setRegNo(Integer regNo) {
this.regNo = regNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
}<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="student">
<select id="selectAll" resultType="com.imooc.entity.Student">
select * from student order by reg_no;
</select>
<insert id="insert" parameterType="com.imooc.mybatis.entity">
INSERT INTO student(reg_no,name,sex,age,grade,major)
VALUES (#{regNo},#{name},#{sex},#{age},#{grade},#{major});
<selectKey resultType="Integer" keyProperty="id" order="AFTER">
select last_insert_id()
</selectKey>
</insert>
</mapper>package com.imooc;
import com.imooc.entity.Student;
import com.imooc.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class WorkTest {
@Test
public void selectAll() throws Exception {
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtils.openSession();
List<Student> list = sqlSession.selectList("student.selectAll");
System.out.println("用户编号"+" "+"学号"+" "+"姓名"+" "+"年龄"+" "+"性别"+" "+"年级"+" "+"专业");
for (Student s : list ){
System.out.println(s.getId()+" "+s.getregNo()+" "+s.getName()+" "+s.getAge()+" "+s.getSex()+" "+s.getGrade()+" "+s.getMajor());
}
} catch (Exception e) {
throw e;
}finally{
MybatisUtils.closeSession(sqlSession);
}
}
@Test
public void testInsert() throws Exception{
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtils.openSession();
Student student = new Student();
student.setRegNo(20171208);
student.setName("言语");
student.setSex("男");
student.setAge(26);
student.setGrade("2013");
student.setMajor("哲学系");
int num =sqlSession.insert("student.insert",student);
sqlSession.commit();
} catch (Exception e) {
if(sqlSession != null){
sqlSession.rollback();
}
throw e;
}finally {
MybatisUtils.closeSession(sqlSession);
}
}
}8
收起
正在回答
1回答
同学你好!
确认一下,同学目前的问题是以下两个问题吗?
使用int和Integer的区别
insert插入报错
如果是:
这里使用int和Integer都是一样的。
insert问题,你的报错信息是找不到下面的类

你这里应该是要写Student类吧?你写成Student测试一下

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
3. Java 数据库开发与实战应用
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程


恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星