create table tb_student
(
student_id int primary key auto_increment,
name varchar(20),
sex char(1),
birthday date
);
select * from tb_student;
insert into tb_student
(name,sex,birthday)
select '张三','男','2001-01-01' UNION
select '李四','女','2002-01-02' union
select '王五','男','2002-01-01' union
select '赵六','男','2003-01-03' union
select '孙琪','男','2002-01-01' union
select '高达','女','2005-01-01' union
select '郭靖','男','2002-11-01' union
select '黄蓉','女','2002-09-01' union
select '大武','男','2002-01-05';
4.0.0
org.example
mybatis_0822_KTLX2
1.0-SNAPSHOT
8
8
5.3.14
1.4
3.4.6
1.3.3
8.0.11
org.springframework
spring-context
${spring.version}
repository.org.springframework
spring-jdbc
${spring.version}
commons-dbcp
commons-dbcp
${commons-dbcp.version}
org.mybatis
mybatis
${mybatis.version}
mysql
mysql-connector-java
${mysql-connector-java.version}
org.mybatis
mybatis-spring
${mybatis-spring.version}
com.github.pagehelper
pagehelper
5.1.4
com.github.abel533
mapper
3.0.1
package com.entity;
import java.util.Date;
public class Student {
private Integer studentId;
private String name;
private String sex;
private Date birthday;
public Student() {
}
public Student(Integer studentId, String name, String sex, Date birthday) {
this.studentId = studentId;
this.name = name;
this.sex = sex;
this.birthday = birthday;
}
public Integer getStudentId() {
return studentId;
}
public void setStudentId(Integer studentId) {
this.studentId = studentId;
}
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 Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Student{" +
"studentId=" + studentId +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", birthday=" + birthday +
'}';
}
}
package com.mapper;
import com.entity.Student;
import java.util.List;
public interface StudentMapper {
List listAll();
}
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/70816_db?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8&allowPublicKeyRetrieval=true
jdbc.username=root
jdbc.password=123456
package com.test;
import com.mapper.StudentMapper;
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.InputStream;
public class Test {
public static void main(String[] args) throws IOException {
InputStream inputStream= Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session=factory.openSession();
StudentMapper studentMapper=session.getMapper(StudentMapper.class);
System.out.println(studentMapper.listAll());
session.commit();
session.close();
}
}
参考:第三章 DAO模式《分层》 ② 代码https://blog.csdn.net/Liu_wen_wen/article/details/125967616
package com.service;
import com.entity.Student;
import java.util.List;
public interface IStudentService {
List listAll();
}
package com.service.impl;
import com.entity.Student;
import com.mapper.StudentMapper;
import com.service.IStudentService;
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.InputStream;
import java.util.List;
public class StudentServiceImpl implements IStudentService {
@Override
public List listAll() {
InputStream inputStream=null;
try {
inputStream= Resources.getResourceAsStream("mybatis-config.xml"); //ctrl+alt+t try+catch
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session=factory.openSession();
StudentMapper studentMapper=session.getMapper(StudentMapper.class);
//System.out.println(studentMapper.listAll());
List studentList=studentMapper.listAll();
session.commit();
session.close();
return studentList;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
package com.test;
import com.entity.Student;
import com.service.IStudentService;
import com.service.impl.StudentServiceImpl;
import java.util.List;
public class Test2 {
public static void main(String[] args) {
IStudentService iStudentService=new StudentServiceImpl();
List studentList=iStudentService.listAll();
System.out.println(studentList);
}
}
package com.test;
import com.entity.Student;
import com.service.IStudentService;
import com.service.impl.StudentServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class Test2 {
public static void main(String[] args) {
// IStudentService iStudentService=new StudentServiceImpl();
// List studentList=iStudentService.listAll();
// System.out.println(studentList);
ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-1.xml");
IStudentService service=ctx.getBean(IStudentService.class);
System.out.println(service.listAll());
}
}
package com.service.impl;
import com.entity.Student;
import com.mapper.StudentMapper;
import com.service.IStudentService;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@Service
public class StudentServiceImpl implements IStudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public List listAll() {
return studentMapper.listAll();
// InputStream inputStream=null;
// try {
// inputStream= Resources.getResourceAsStream("mybatis-config.xml"); //ctrl+alt+t try+catch
// SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
// SqlSession session=factory.openSession();
// StudentMapper studentMapper=session.getMapper(StudentMapper.class);
// //System.out.println(studentMapper.listAll());
// List studentList=studentMapper.listAll();
//
// session.commit();
// session.close();
//
// return studentList;
// } catch (IOException e) {
// e.printStackTrace();
// }
// return null;
}
}
// A code block
var foo = 'bar';
// A code block
var foo = 'bar';