1、针对以下数据表
Id |
name |
addr |
age |
job |
sal |
1 |
张三 |
江苏省南京市 |
35 |
区域总监 |
20000 |
2 |
李四 |
江苏省苏州市 |
32 |
销售经理 |
15000 |
3 |
王五 |
四川省成都市 |
26 |
销售员 |
8000 |
4 |
赵六 |
吉林省长春市 |
27 |
财务 |
10000 |
5 |
刘七 |
吉林省吉林市 |
24 |
销售员 |
6000 |
6 |
吴八 |
陕西省西安市 |
31 |
销售员 |
7000 |
使用mybatis逆向工程,生成对应的model和mapper处理
2、调用逆向工程API向其中插入数据
Id |
name |
addr |
age |
job |
sal |
7 |
张丽 |
江苏省南京市 |
32 |
销售员 |
7000 |
8 |
张伟 |
江苏省苏州市 |
26 |
职员 |
5000 |
9 |
王晓 |
上海市 |
30 |
区域总监 |
20000 |
10 |
赵敏 |
上海市 |
25 |
财务 |
10000 |
3、调用逆向工程API及分页插件,使用查询江苏省员工信息,每3条数据一页
4、调用逆向工程API将所有27岁以上的销售员,工资加1000元
5、调用逆向工程API将陕西省员工删除
<?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> <classPathEntry location="C:\JavaHome\m2\mysql\mysql-connector-java\8.0.26\mysql-connector-java-8.0.26.jar" /> <context id="mysqlTables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useUnicode=true&serverTimezone=GMT%2B8&useSSL=false" userId="root" password="zengyu1234"> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <javaModelGenerator targetPackage="com.xzit.model" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="com.xzit.mapper" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.xzit.mapper" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table tableName="teacher"></table> </context> </generatorConfiguration>
package com.xzit.test; import com.xzit.mapper.TeacherMapper; import com.xzit.model.Teacher; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; public class TestInsertTeacher { public static void main(String[] args) throws IOException { SqlSession session = new SqlSessionFactoryBuilder().build( Resources.getResourceAsStream("SqlMapConfig.xml")).openSession(); TeacherMapper mapper=session.getMapper(TeacherMapper.class); mapper.insert(new Teacher(0,"张丽","江苏省南京市",32,"销售员",7000)); mapper.insert(new Teacher(0,"张伟","江苏省苏州市",26,"职员",5000)); mapper.insert(new Teacher(0,"王晓","上海市",30,"区域总监",20000)); mapper.insert(new Teacher(0,"赵敏","上海市",25,"财务",10000)); session.commit(); session.close(); } }
package com.xzit.test; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.xzit.mapper.TeacherMapper; import com.xzit.model.Teacher; import com.xzit.model.TeacherExample; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.util.List; /*3、调用逆向工程API及分页插件,使用查询江苏省员工信息,每3条数据一页*/ public class TestSelect { public static void main(String[] args) throws IOException { SqlSession session = new SqlSessionFactoryBuilder().build( Resources.getResourceAsStream("SqlMapConfig.xml")).openSession(); TeacherMapper mapper=session.getMapper(TeacherMapper.class); //使用分页查询插件,从第1页开始,每页显示2个 PageHelper.startPage(2,2); TeacherExample example = new TeacherExample(); example.createCriteria().andAddrLike("%江苏省%"); List<Teacher> list = mapper.selectByExample(example); //将list封装到分页查询对象中 PageInfo<Teacher> page = new PageInfo<>(list); //使用list接收查询结果 List<Teacher> teachers = page.getList(); teachers.forEach(System.out::println); session.close(); } }
package com.xzit.test; import com.xzit.mapper.TeacherMapper; import com.xzit.model.Teacher; import com.xzit.model.TeacherExample; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.util.List; /*4、调用逆向工程API将所有27岁以上的销售员,工资加1000元*/ public class TestUpdate { public static void main(String[] args) throws IOException { SqlSession session = new SqlSessionFactoryBuilder().build( Resources.getResourceAsStream("SqlMapConfig.xml")).openSession(); TeacherMapper mapper=session.getMapper(TeacherMapper.class); //创建查询对象,先查询出27岁以上的员工 TeacherExample example = new TeacherExample(); example.createCriteria().andAgeGreaterThan(27); //将超过27岁的员工存进list List<Teacher> list = mapper.selectByExample(example); //使用for遍历符合条件的每一位员工 for (Teacher teacherlist:list){ //将该员工的工资加1000,对象封装成teacherlist teacherlist.setSal(teacherlist.getSal()+1000); //创建一个对象2 TeacherExample example2 = new TeacherExample(); //设置查询条件是该员工的id,查到数据库的这个员工 example2.createCriteria().andIdEqualTo(teacherlist.getId()); //使用teacherlist更新这个员工的信息 mapper.updateByExampleSelective(teacherlist,example2); //提交事务 session.commit(); } session.close(); } }
package com.xzit.test; import com.xzit.mapper.TeacherMapper; import com.xzit.model.TeacherExample; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; /*5、调用逆向工程API将陕西省员工删除*/ public class TestDelete { public static void main(String[] args) throws IOException { SqlSession session = new SqlSessionFactoryBuilder().build( Resources.getResourceAsStream("SqlMapConfig.xml")).openSession(); TeacherMapper mapper=session.getMapper(TeacherMapper.class); TeacherExample example = new TeacherExample(); example.createCriteria().andAddrLike("%陕西省%"); mapper.deleteByExample(example); session.commit(); session.close(); } }