网站建设人员的组织,辽宁省建设工程信息网必须用主锁,做家政网站公司,网络营销服务公司这里写目录标题 一、项目结构1、model模型类Student2、mapper 数据访问层接口和映射文件接口类StudentMapper接下来创建名为 StudentMapper.xml 的映射文件 3、service 服务层接口和实现类创建名为 StudentService 的 Java 接口创建名为 StudentServiceImpl 的实现类 4、contro… 这里写目录标题 一、项目结构1、model模型类Student2、mapper 数据访问层接口和映射文件接口类StudentMapper接下来创建名为 StudentMapper.xml 的映射文件 3、service 服务层接口和实现类创建名为 StudentService 的 Java 接口创建名为 StudentServiceImpl 的实现类 4、controller5、配置 resources5-1、mapper5-2、mysqldb.properties5-3、spring.xml5-4、springmvc.xml controller 存放控制器Controller类处理请求和返回响应。 mapper 存放数据访问层Mapper接口和对应的XML文件。 model 存放模型Model类与数据库表对应。 service 存放服务Service接口和实现类 impl
resources mapper文件 存放Mapper接口对应的XML文件定义SQL语句和映射关系。 mybatis-config.xmlMyBatis的主配置文件配置数据库连接等全局设置。 db.properties数据库连接信息的配置文件。
project-name
├── src
│ ├── main
│ │ ├── java # Java代码目录
│ │ │ ├── com.example.project.controller # 控制器包
│ │ │ ├── com.example.project.mapper # Mapper接口和XML文件包
│ │ │ ├── com.example.project.model # 模型包
│ │ │ ├── com.example.project.service # 业务逻辑包
│ │ │ └── com.example.project.util # 工具类包
│ │ ├── resources # 资源目录
│ │ │ ├── mapper # 存放Mapper对应的XML文件
│ │ │ ├── mybatis-config.xml # MyBatis主配置文件
│ │ │ └── db.properties # 数据库连接配置文件
│ │ └── webapp # Web资源目录
│ │ ├── WEB-INF # Web应用配置目录
│ │ │ ├── web.xml # Web应用配置文件
│ │ │ └── spring.xml # Spring配置文件如果使用Spring
│ │ ├── index.jsp # 首页JSP文件
│ │ └── static # 静态资源目录
│ │ ├── css # CSS文件目录
│ │ ├── js # JavaScript文件目录
│ │ └── images # 图片目录
└── pom.xml # Maven项目配置文件
一、项目结构
1、model模型类Student
public class Student {private Long id;private String name;private Integer age;private String gender;// getter 和 setter 方法省略
}
2、mapper 数据访问层接口和映射文件
接口类StudentMapper
// 2. 定义数据访问层接口和映射文件package com.example.mapper;public interface StudentMapper {void insert(Student student); // 插入学生信息void delete(Long id); // 删除学生信息void update(Student student); // 更新学生信息Student selectById(Long id); // 根据ID查询学生信息ListStudent selectAll(); // 查询所有学生信息
}
接下来创建名为 StudentMapper.xml 的映射文件
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
!-- StudentMapper.xml --
!-- 映射SQL语句和Java对象之间的关系 --
mapper namespacecom.example.mapper.StudentMapper!-- 定义结果映射 --resultMap idStudentResultMap typecom.example.model.Studentid columnid propertyid/result columnname propertyname/result columnage propertyage/result columngender propertygender//resultMap!-- 插入学生信息 --insert idinsert parameterTypecom.example.model.StudentINSERT INTO student(name, age, gender) VALUES(#{name}, #{age}, #{gender})/insert!-- 删除学生信息 --delete iddelete parameterTypejava.lang.LongDELETE FROM student WHERE id#{id}/delete!-- 更新学生信息 --update idupdate parameterTypecom.example.model.StudentUPDATE student SET name#{name}, age#{age}, gender#{gender} WHERE id#{id}/update!-- 根据ID查询学生信息 --select idselectById resultMapStudentResultMapSELECT * FROM student WHERE id#{id}/select!-- 查询所有学生信息 --select idselectAll resultMapStudentResultMapSELECT * FROM student/select/mapper
3、service 服务层接口和实现类
创建名为 StudentService 的 Java 接口
// 3. 定义服务层接口和实现类package com.example.service;public interface StudentService {void add(Student student); // 添加学生信息void remove(Long id); // 删除学生信息void modify(Student student); // 修改学生信息Student findById(Long id); // 根据ID查询学生信息ListStudent findAll(); // 查询所有学生信息
}
创建名为 StudentServiceImpl 的实现类
Service
public class StudentServiceImpl implements StudentService {Autowiredprivate StudentMapper studentMapper;Overridepublic void add(Student student) {studentMapper.insert(student);}Overridepublic void remove(Long id) {studentMapper.delete(id);}Overridepublic void modify(Student student) {studentMapper.update(student);}Overridepublic Student findById(Long id) {return studentMapper.selectById(id);}Overridepublic ListStudent findAll() {return studentMapper.selectAll();}
}
4、controller
创建名为 StudentController 的 Java 类
// 4. 编写控制器类Controller
public class StudentController {Autowiredprivate StudentService studentService;// 处理显示学生列表的请求GetMapping(/students)public ModelAndView showStudents() {ListStudent students studentService.findAll();ModelAndView mav new ModelAndView(students); // 返回视图名为studentsmav.addObject(students, students); // 将学生列表添加到模型中return mav;}// 处理显示添加学生页面的请求GetMapping(/students/new)public ModelAndView showAddStudentForm() {ModelAndView mav new ModelAndView(add_student_form); // 返回视图名为add_student_formmav.addObject(student, new Student()); // 创建一个空的学生对象并添加到模型中return mav;}// 处理提交添加学生的请求PostMapping(/students)public String addStudent(ModelAttribute(student) Student student) {studentService.add(student);return redirect:/students; // 重定向到学生列表页面}// 处理显示修改学生页面的请求GetMapping(/students/{id}/edit)public ModelAndView showEditStudentForm(PathVariable Long id) {Student student studentService.findById(id);ModelAndView mav new ModelAndView(edit_student_form); // 返回视图名为edit_student_formmav.addObject(student, student); // 将要修改的学生对象添加到模型中return mav;}// 处理提交修改学生的请求PutMapping(/students/{id})public String updateStudent(PathVariable Long id, ModelAttribute(student) Student student) {student.setId(id);studentService.modify(student);return redirect:/students; // 重定向到学生列表页面}// 处理删除学生的请求DeleteMapping(/students/{id})public String deleteStudent(PathVariable Long id) {studentService.remove(id);return redirect:/students; // 重定向到学生列表页面}
}
5、配置 resources
在resources进行配置
5-1、mapper
在resources新建一个mapper文件 存放Mapper接口对应的XML文件定义SQL语句和映射关系。
5-2、mysqldb.properties
数据库连接信息的配置文件。
drivercom.mysql.jdbc.Driver
urljdbc:mysql://localhost:3306/student?useSSLfalse
userroot
pwd1234567895-3、spring.xml
配置 Spring 容器和 MyBatis 相关内容如数据源、事务管理等
!-- 配置数据源 --
bean iddataSource classcom.zaxxer.hikari.HikariDataSourceproperty namedriverClassName value${spring.datasource.driver-class-name}/property namejdbcUrl value${spring.datasource.url}/property nameusername value${spring.datasource.username}/property namepassword value${spring.datasource.password}/property nameminimumIdle value${spring.datasource.hikari.minimumIdle}/property namemaximumPoolSize value${spring.datasource.hikari.maximumPoolSize}/
/bean!-- 配置 SqlSessionFactory --
bean idsqlSessionFactory classorg.mybatis.spring.SqlSessionFactoryBeanproperty namedataSource refdataSource/property namemapperLocations valueclasspath:mapper/*.xml/
/bean!-- 配置 MapperScannerConfigurer --
bean classorg.mybatis.spring.mapper.MapperScannerConfigurerproperty namebasePackage valuecom.example.mapper/
/bean!-- 配置事务管理器 --
bean idtransactionManager classorg.springframework.jdbc.datasource.DataSourceTransactionManagerproperty namedataSource refdataSource/
/bean!-- 开启基于注解的事务 --
tx:annotation-driven transaction-managertransactionManager/
5-4、springmvc.xml
配置 SpringMVC 相关内容例如视图解析器、处理器映射等
mvc:annotation-driven/bean idviewResolverclassorg.springframework.web.servlet.view.InternalResourceViewResolverproperty nameprefix value/WEB-INF/jsp//property namesuffix value.jsp/
/beanmvc:resources mapping/static/** location/static//mvc:interceptorsmvc:interceptormvc:mapping path/**/bean classcom.example.interceptor.TimeInterceptor/bean/mvc:interceptor
/mvc:interceptors