在pom.xml文件中导入所需要的坐标
org.mybatis
mybatis
3.5.9
public class Student {
private int stuId;
private String stuName;
private String stuHobby;
private String stuAge;
@Override
public String toString() {
return "Student{" +
"stuId=" + stuId +
", stuName='" + stuName + '\'' +
", stuHobby='" + stuHobby + '\'' +
'}';
}
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuHobby() {
return stuHobby;
}
public void setStuHobby(String stuHobby) {
this.stuHobby = stuHobby;
}
public Student(){
}
public Student(String stuName, String stuHobby) {
this.stuName = stuName;
this.stuHobby = stuHobby;
}
public Student(int stuId, String stuName, String stuHobby) {
this.stuId = stuId;
this.stuName = stuName;
this.stuHobby = stuHobby;
}
}
StudentMapper文件:
public interface StudentMapper {
@Insert("insert into student(stu_name,stu_hobby) values(#{stuName},#{stuHobby})")
public void saveStu(Student student);
@Select("select * from student")
public List findAll();
}
Application.xml文件:
public interface StudentMapper {
@Insert("insert into student(stu_name,stu_hobby) values(#{stuName},#{stuHobby})")
public void saveStu(Student student);
@Select("select * from student")
public List findAll();
}
public class Test01 {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection root = DriverManager.getConnection("jdbc:mysql://localhost:3306/230809db?serverTimezone=GMT", "root", "123456");
System.out.println(root);
} catch (ClassNotFoundException exception) {
exception.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
JavaBean |
DB |
stuId | stu_id |
stuName | stu_name |
stuHobby | stu_hobby |
开启自动驼峰映射步骤:配置文件中配置mapUnderscoreToCamelCase
含义:junit专业测试方法的工具
注意:在test目录下的java目录下创建包的时候,包的名称必须与main下的java目录里面的父包名称一致
junit
junit
4.13.2
test
@Test=========》main
@Before=======》在@Test直接执行之前运行
@After========》在@Test直接执行之后运行
public class Test01 {
InputStream inputStream = null;
SqlSession sqlSession = null;
StudentMapper mapper = null;
@Before
public void beforeMethod(){
try {
inputStream = Resources.getResourceAsStream("Application.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = sqlSessionFactoryBuilder.build(inputStream);
sqlSession = factory.openSession(true);
mapper = sqlSession.getMapper(StudentMapper.class);
} catch (IOException e) {
e.printStackTrace();
}
}
@After
public void afterMethod(){
try {
sqlSession.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void findAll(){
List all = mapper.findAll();
for (int i = 0; i < all.size(); i++) {
Student student = all.get(i);
System.out.println(student);
}
}
@Test
public void save(){
Student student1 = new Student("张三","敲代码~");
Student student2 = new Student("李四","唱跳rap");
Student student3 = new Student("王五","篮球");
mapper.saveStu(student1);
mapper.saveStu(student2);
mapper.saveStu(student3);
}
}
org.projectlombok
lombok
1.18.28
com.baomidou
mybatis-plus
3.1.1
注意:mp坐标添加后,mybatis坐标移除
@TableName(value = "关联表名称") 修饰在类
@TableField(value = "关联字段名称") 修饰在属性
exist = "忽略字段"
@TableId(type="指定主键生成策略,默认雪花算法") 修饰在属性
@TableName(value = "student")
public class Student {
@TableId(value = "stuid",type = IdType.AUTO)
private int stuId;
@TableField(value = "stuname")
private String stuName;
@TableField(value = "stuhobby")
private String stuHobby;
@TableField(value = "stuage")
private String stuAge;
public Student(String stuName, String stuHobby) {
this.stuName = stuName;
this.stuHobby = stuHobby;
}
}
BaseMapper 公共的数据访问层
IService/ServiceImp 公共的业务层
public interface StudentMapper extends BaseMapper {
@Insert("insert into student(stuname,stuhobby) value(#{stuName},#{stuHobby})")
public void saveStu(Student student);
@Select("select * from student")
public List findAll();
}
public class Test03 {
InputStream inputStream = null;
SqlSession sqlSession = null;
StudentMapper mapper = null;
@Before
public void beforeMethod(){
try {
//1.加载资源文件
inputStream = Resources.getResourceAsStream("mybatis.xml");
//2.创建构造器
MybatisSqlSessionFactoryBuilder builder = new MybatisSqlSessionFactoryBuilder();
//3.创建工厂
SqlSessionFactory factory = builder.build(inputStream);
//4.获取会话
sqlSession = factory.openSession(true);
//5.加载映射器
mapper = sqlSession.getMapper(StudentMapper.class);
} catch (IOException e) {
e.printStackTrace();
}
}
@After
public void afterMethod(){
try {
sqlSession.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void findAll(){
List all = mapper.findAll();
for(int i = 0; i< all.size();i++){
Student student = all.get(i);
System.out.println(student);
}
}
@Test
public void show1(){
//1.条件查询
LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper();
lambdaQueryWrapper.eq(Student::getStuName,"123");
//2.查询
List list = mapper.selectList(lambdaQueryWrapper);
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
System.out.println(student);
}
}
//模拟动态查询1
@Test
public void test02(){
Integer num1 = null;
Integer num2 = 30;
//1.查询条件
LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper();
//2.判断
if (null != num2){
lambdaQueryWrapper.lt(Student::getStuAge,num2);
}
if (null != num2){
lambdaQueryWrapper.gt(Student::getStuAge,num1);
}
//3.查询
List list = mapper.selectList(lambdaQueryWrapper);
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
System.out.println(student);
}
}
//模拟动态查询2
@Test
public void show3(){
//1.前端发送来的数据
Integer num1 = null;
Integer num2 = 30;
//1.查询条件
LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper();
//2.判断
lambdaQueryWrapper.lt(null != num2, Student::getStuAge,num2);
lambdaQueryWrapper.gt(null != num1, Student::getStuAge,num1);
//3.查询
List list = mapper.selectList(lambdaQueryWrapper);
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
System.out.println(student);
}
}
//投影查询-字段查询
@Test
public void show4() {
//1.条件
LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper();
lambdaQueryWrapper.select(Student::getStuName, Student::getStuHobby);
//2.查询
List list = mapper.selectList(null);
//4.遍历
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
System.out.println(student);
}
}
//投影查询-聚合查询
@Test
public void show5() {
//1.查询条件
QueryWrapper queryWrapper = new QueryWrapper();
//2.判断
queryWrapper.select("count(*) as num1,sum(stu_age) as num2,max(stu_age) as num3");
//3.查询
List