实验报告
4、使用MyBatis逆向工程,生成mapper接口和映射文件
5、执行逆向工程
6、修正MyBatis自动生成的接口文件EmployeeMapper.java p.mapper;
p.model.Employee;
p.model.EmployeeExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface EmployeeMapper {
long countByExample(EmployeeExample example);
int deleteByExample(EmployeeExample example);
int deleteByPrimaryKey(Integer empId);
int insert(Employee record);
int insertSelective(Employee record);
//逆向工程之后,手动增加,校验用户登录方法
Employee checkUser(String username, String password);
List<Employee> selectByExample(EmployeeExample example);
//逆向工程之后,手动增加,获取全体员工信息方法
List<Employee> getAllEmployee(EmployeeExample example);
//逆向工程之后,手动增加,获取数据库总行数方法
int getTotalCount();
//逆向工程之后,手动增加,分页方法
List<Employee> getAllEmployeeByPage(int currentPage, int pageSize);
// 以上为手动添加的五个方法
Employee selectByPrimaryKey(Integer empId);
int updateByExampleSelective(@Param("record") Employee
record, @Param("example") EmployeeExample
example);
int updateByExample(@Param("record") Employee record,
@Param("example") EmployeeExample example);
int updateByPrimaryKeySelective(Employee record);
int updateByPrimaryKey(Employee record);
}
7、MyBatis属于面向接口编程,需要同步修正对于的XML文件<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "/dtd/mybatis-3-
mapper.dtd">
<mapper namespace="p.mapper.EmployeeMapper">
<resultMap id="BaseResultMap" type="del.Employee">        <id column="emp_id" jdbcType="INTEGER" property="empId"/>        <result column="emp_name" jdbcType="VARCHAR"
property="empName"/>
<result column="emp_gender" jdbcType="CHAR"
property="empGender"/>
<result column="emp_password" jdbcType="VARCHAR" property="empPassword"/>
<result column="emp_grade" jdbcType="VARCHAR"
property="empGrade"/>
<result column="emp_email" jdbcType="VARCHAR"
property="empEmail"/>
<result column="emp_department_id" jdbcType="INTEGER" property="empDepartmentId"/>
</resultMap>
以下为手工添加部分:
<!-- 此处sql语句为自行添加,逆向工程没有生成 -->
<select id="checkUser" parameterType="String"
resultType="del.Employee">
select * from employee where emp_name=#{0} and
emp_password=#{1}
</select>
<!-- 此处sql语句为自行添加,逆向工程没有生成 -->
<select id="getTotalCount" resultType="java.lang.Integer">        select count(*) from employee
</select>
<!-- 此处sql语句为自行添加,逆向工程没有生成 -->
<select id="getAllEmployeeByPage" parameterType="Integer" resultMap="employee">
select * from employee e LEFT JOIN department d on
limit #{0},#{1}
</select>
<!-- 此处sql语句为自行添加,逆向工程没有生成 -->
<select id="getAllEmployee"
parameterType="del.EmployeeExample"
resultMap="employee">
<!-- 使用的是左外连接,不要使用子查询 -->
select * from employee e LEFT JOIN department d on
limit 0,10
</select>
8、整合Spring与MyBatis
8.1、在资源文件夹下置入MyBatis的配置文件l
8.2、在资源文件夹下置入
(l,Spring核心配置文件。负责加载数据库配置文件,数据库连接池,配置SqlSessionFactory工厂,以及配置MapperScannerConfigurer扫描mapper包
(l,Spring配置文件,负责扫描service包
java spring框架搭建(l,Spring配置文件,负责事务管理
(4)db.properties数据库配置文件
(5)log4j.properties日志配置文件
9、整合 Spring MyBatis 和 SpringMVC
置入 l 配置文件:
(1)配置 controller 扫描包
(2) 注解驱动自动注册 RequestMappingHandlerMapping、RequestMappingHandlerAdapter 等
bean
(3) 静态资源处理
(4) 配置视图解析器
(5) 定义文件上传解析器*
10、创建 Service 接口
11、创建接口实现类
12、配置l
13、在WEB-INF下创建views文件夹,保存*.jsp文件
可以首先编写简单jsp,测试SSM项目构建是否成功。
14、在webapp文件夹下创建bootstrap_plugin、images、mycss 文件夹,分别置入相应资源文件
15、在webapp文件夹下创建index.jsp文件
16、在webapp/WEB-INF/views文件夹下创建showEmployee.jsp 文件
分别引入头部,左部和脚步 head.jsp main-left.jsp footer.jsp
17、考虑显示页面分页问题,加入PageBean
p.model;
import java.util.List;
public class PageBean {
//程序员指定或者页面参数
private int currentPage;//当前页
private int pageSize; //每页显示多少条
//查询数据库
private int recordCount;//数据库一共有多少条
private List recordList;//本页显示的数据列表
//计算
private int pageCount; //计算后得到的数值,总页数
private int beginPageIndex;//页面列表的开始索引(包括)
private int endPageIndex; //页面列表的结束索引(包括)
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getRecordCount() {
return recordCount;
}
public void setRecordCount(int recordCount) {