Mybatis 逆向工程
-
环境搭建,GeneratorSqlmap.java 逆向工程程序执行入口
-
配置文件说明
generatorConfig.xml : 逆向工程配置文件
高亮部分是需要配置的部分,配置结束后到GeneratorSqlmap.java执行即可
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/css" userId="root"
password="888">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- targetProject:生成POJO类的位置 -->
<!--POJO类就是实体类-->
<javaModelGenerator targetPackage="com.guifei.entity"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.guifei.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.guifei.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 指定数据库表 -->
<table tableName="student" schema="student"></table>
<table tableName="course" schema="course"></table>
<table tableName="choose" schema="choose"></table>
<table tableName="studentclass" schema="studentclass"></table>
<table tableName="teacher" schema="teacher"></table>
</context>
</generatorConfiguration>
- 如何使用
1.将生成好的实体类、接口、映射文件拷入到自己的工程中即可
2.具体使用
/**
* 学生表数据操作接口测试类
*/
public class StudentMapperTest {
SqlSession sqlSession;
StudentMapper studentMapper;
@Before
public void SetUp(){
sqlSession = mybatisUtils.getSqlSession();
studentMapper = sqlSession.getMapper(StudentMapper.class);
TestUtils.printByDebug(this.getClass(), "测试开始");
}
@After
public void tearDown(){
TestUtils.printByDebug(this.getClass(), "测试结束");
}
//向学生表中增加一个学生
@Test
public void testInsertStudent(){
Student student = new Student(null, "张红霞", "女", 1, null, null);
studentMapper.insert(student);
sqlSession.commit();
}
//向学生表中删除通过stuid一个学生
@Test
public void testdelectStudent(){
studentMapper.deleteByPrimaryKey(12);
sqlSession.commit();
}
//修改表中学生的数据
@Test
public void testUpdateStudent(){
Student student = new Student(1, "张四", "女", 1, null, null);
studentMapper.updateByPrimaryKey(student);
sqlSession.commit();
}
//根据stuid查询学生表中学生
@Test
public void testselectStudentBystuid(){
Student student = studentMapper.selectByPrimaryKey(1);
TestUtils.printByDebug(this.getClass(), student);
}
//通过条件查询,查询所有学生,并分页展示
@Test
public void testselectAllStudentByPage(){
StudentExample studentExample = new StudentExample();
//分页插件的使用,将分页放到查询之前
PageHelper.startPage(1,2);
List<Student> students = studentMapper.selectByExample(studentExample);
// System.out.println(students);
//将查询到的数据封装到pageinfo中
PageInfo<Student> studentPageInfo = new PageInfo<>(students);
//将查询数据取出遍历
List<Student> studentList = studentPageInfo.getList();
for (Student student : studentList) {
System.out.println(student);
}
// 取分页信息
PageInfo<Student> pageInfo = new PageInfo<Student>(students);
long total = pageInfo.getTotal(); //获取总记录数
TestUtils.printByDebug(this.getClass(), "学生总数:" + total);
}
//根据stuid查询学生表中学生
@Test
public void testselectStudent(){
//条件查询,1.创建StudentExample对象
StudentExample studentExample = new StudentExample();
//2.创建Criteria对象
StudentExample.Criteria criteria = studentExample.createCriteria();
//3.创建查询条件
criteria.andStunameLike("%李四%");
//4.将查询条件传给接口,执行查询
List<Student> students = studentMapper.selectByExample(studentExample);
TestUtils.printByDebug(this.getClass(), students);
}
//根据stuid查询学生信息,并查出学生属于哪个班级
@Test
public void testselectStudentAanClass(){
//现根据学生id查出学生的信息,找到班级id查询班级,再将班级信息赋值给学生
Student student = studentMapper.selectByPrimaryKey(1);
StudentclassMapper studentclassMapper = sqlSession.getMapper(StudentclassMapper.class);
Studentclass studentclass = studentclassMapper.selectByPrimaryKey(student.getClaid());
student.setStudentclass(studentclass);
TestUtils.printByDebug(this.getClass(), student);
}
//查询所有学生信息,并查出学生属于哪个班级
@Test
public void testselectAllStudentAanClass(){
//现根据学生id查出学生的信息,找到班级id查询班级,再将班级信息赋值给学生
List<Student> students = studentMapper.selectByExample(null);
StudentclassMapper studentclassMapper = sqlSession.getMapper(StudentclassMapper.class);
for (Student student : students) {
Studentclass studentclass = studentclassMapper.selectByPrimaryKey(student.getClaid());
student.setStudentclass(studentclass);
}
TestUtils.printByDebug(this.getClass(), students);
}
//根据学生stuid查询学生信息以及所学课程
@Test
public void testselectStudentAanCourseByid(){
//现根据学生id查出学生的信息,再根据选课表查找到所有的选课记录,再用couid查找课程
List<Student> students = studentMapper.selectByExample(null);
ChooseMapper chooseMapper = sqlSession.getMapper(ChooseMapper.class);
CourseMapper courseMapper = sqlSession.getMapper(CourseMapper.class);
ChooseExample chooseExample = new ChooseExample();
CourseExample courseExample = new CourseExample();
for (Student student : students) {
//每查询一下都要创建一个查询条件
ChooseExample.Criteria criteria = chooseExample.createCriteria();
criteria.andStuidEqualTo(student.getStuid());
List<Choose> chooses = chooseMapper.selectByExample(chooseExample);
for (Choose choose : chooses) {
//每查询一下都要创建一个查询条件
CourseExample.Criteria criteria1 = courseExample.createCriteria();
criteria1.andCouidEqualTo(choose.getCouid());
List<Course> courses = courseMapper.selectByExample(courseExample);
student.setCourses(courses);
}
// student.setCourses(courses);
}
TestUtils.printByDebug(this.getClass(), students);
}
}
- 创建SqlSession的工具来 MybatisUtils.java
/**
* mybatis工具类
*/
public class mybatisUtils {
static SqlSessionFactory sqlSessionFactory=null;
//在工具类第一次加载时创建sqlSessionFactory
static {
try {
InputStream inputStream = Resources.getResourceAsStream("mybatisconfig.xml");
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
sqlSessionFactory=builder.build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
}