创建包,配置文件,自定义字符编码过滤器
dao(接口),entity(pojo实体类),global(工具类)
spring-dao.xml dao配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="cn.com.*">context:component-scan>
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver">property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/workordermanager?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8">property>
<property name="username" value="root">property>
<property name="password" value="root">property>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="myDataSource">property>
<property name="mapperLocations">
<list>
<value>classpath:cn/com/dao/ProjectMapper.xmlvalue>
list>
property>
<property name="typeAliasesPackage" value="cn.com.entity">property>
<property name="configLocation" value="classpath:mybatis-config.xml">property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.com.dao">property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
bean>
beans>
spring-biz.xml biz配置文件(service层)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<import resource="spring-dao.xml">import>
<context:component-scan base-package="cn.com.*">context:component-scan>
<tx:annotation-driven proxy-target-class="true" transaction-manager="txManage">tx:annotation-driven>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean id="txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="myDataSource"/>
bean>
<tx:advice transaction-manager="txManage" id="txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="SUPPORTS"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
tx:attributes>
tx:advice>
<aop:config>
<aop:pointcut id="pointcut1" expression="execution(* cn.com.biz..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
aop:config>
beans>
3.web层(Controller层)
java下创建包
controller(控制类),dto(pojo实体类),global(工具类)
spring-web.xml web配置文件(Controller层)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<import resource="spring-biz.xml">import>
<mvc:default-servlet-handler />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/">property>
<property name="suffix" value=".jsp">property>
bean>
<!–注解扫描,bean,包名加*通配符–>
<!–拦截器注册–>
<mvc:interceptors>
<!–用户登录拦截器–>
<mvc:interceptor>
<mvc:mapping path="/user/*">mvc:mapping>
<mvc:exclude-mapping path="/user/updatepwd">mvc:exclude-mapping>
<bean class="cn.com.inteceptor.LoginInteceptor">bean>
mvc:interceptor>
<!–日志拦截器–>
mvc:interceptors>
<!–pageHelp分页–>
<!– 配置pageHelp的拦截器 –>
<bean id="plugin" class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>helperDialect=mysqlvalue>
property>
bean>
<bean id="course1" class="cn.com.pojo.Course">
<property name="name" value="新课1">property>
<property name="price" value="666.00">property>
bean>
<bean id="course2" class="cn.com.pojo.Course"
p:name="新课2" p:price="777.00">bean>
<bean id="course3" class="cn.com.pojo.Course"
p:id="6" p:name="改课3" p:price="888.00">bean>
beans>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>Archetype Created Web Applicationdisplay-name>
<filter>
<filter-name>hiddenHttpMethodFilterfilter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
filter>
<filter>
<filter-name>EncodeFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>
<init-param>
<param-name>forceEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>EncodeFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<filter-mapping>
<filter-name>hiddenHttpMethodFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-web.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>defaultservlet-name>
<url-pattern>/assets/*url-pattern>
<url-pattern>/js/*url-pattern>
<url-pattern>/vendor/*url-pattern>
<url-pattern>*.cssurl-pattern>
<url-pattern>*.jsurl-pattern>
<url-pattern>*.jpgurl-pattern>
<url-pattern>*.pngurl-pattern>
<url-pattern>*.gifurl-pattern>
servlet-mapping>
web-app>
*自定义编码过滤器
web.xml 配置文件
global包下创建过滤器类
dao下entity创建实体类(toString,构造方法,get/set)
员工表类
//员工表类
public class Employee {
private String sn; //编号
private String password; //密码
private String name; //姓名
private String department_sn; //所属部门
private String post; //职务
dao下dao包创建Mapper接口
//员工表接口
public interface EmployeeMapper {
//查全部
public List<Employee> getAll();
//根据sn查询
public Employee getById(String sn);
//增加员工
public void add(Employee employee);
//修改员工
public void update(Employee employee);
//删除员工(根据sn)
public void delete(String sn);
}
同时dao下resources创建cn.com.dao同名包
创建Mapper.xml映射文件
<mapper namespace="cn.com.dao.EmployeeMapper">
<select id="getAll" resultType="Employee">
SELECT * FROM `employee`;
select>
<select id="getById" parameterType="String" resultType="Employee">
SELECT * FROM `employee` WHERE sn=#{sn};
select>
<insert id="add" parameterType="Employee">
INSERT INTO `employee` (sn,password,name,department_sn,post) VALUES (#{sn},#{password},#{name},#{department_sn},#{post});
insert>
<update id="update" parameterType="Employee">
UPDATE `employee` SET sn=#{sn},password=#{password},name=#{name},department_sn=#{department_sn},post=#{post} WHERE sn = #{sn};
update>
<delete id="delete" parameterType="String">
DELETE FROM `employee` WHERE sn = #{sn};
delete>
mapper>
biz(Service)下的biz包创建Service接口dao实现类
//员工接口实现类
@Service
public class EmployeeService implements EmployeeMapper {
//注入dao层接口
@Autowired
private EmployeeMapper employeeMapper;
//查全部
public List<Employee> getAll() {
return employeeMapper.getAll();
}
//根据sn查询
public Employee getById(String sn) {
return employeeMapper.getById(sn);
}
//添加
public void add(Employee employee) {
employeeMapper.add(employee);
}
//修改
public void update(Employee employee) {
employeeMapper.update(employee);
}
//删除
public void delete(String sn) {
employeeMapper.delete(sn);
}
}
//Service层测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-web.xml")
public class TestService {
private Logger logger = Logger.getLogger(TestService.class);
//注入部门表Service层接口实现类
@Resource(name = "departmentService")
private DepartmentService departmentService;
//查询部门表
@Test
public void getAllDepartment(){
List<Department> departments = departmentService.getAll();
logger.info("部门信息查询如下:");
for(Department d:departments){
logger.info(d.toString());
}
}
//注入员工表Service层接口实现类
@Resource(name = "employeeService")
private EmployeeService employeeService;
//查询员工表
@Test
public void getAllEmployee(){
List<Employee> employees = employeeService.getAll();
logger.info("员工信息查询如下:");
for(Employee e:employees){
logger.info(e.toString());
}
}
//根据sn查询员工
@Test
public void getById(){
Employee employee = employeeService.getById("10004");
logger.info("全部员工信息查询如下:");
logger.info(employee.toString());
}
//添加员工信息
//导入Spring容器实例
@Resource(name = "employee1")
private Employee employee1;
@Resource(name = "employee2")
private Employee employee2;
@Test
public void add(){
employeeService.add(employee1);
getAllDepartment();
logger.info("添加一条员工信息成功!");
getAllEmployee();
}
//修改员工信息
@Test
public void update(){
Employee employee02 = new Employee("10005","123123","万茜","10003","员工");
employee02.setSn("10005");
employeeService.update(employee02);
logger.info("修改员工信息成功!");
getAllEmployee();
}
//删除一条员工信息
@Test
public void delete(){
employeeService.delete("10005");
logger.info("删除一条员工信息成功!");
getAllEmployee();
}
}
spring-web.xml 实例化对象用于添加数据(依赖实体类无参构造方法)
<bean id="employee1" class="cn.com.entity.Employee" p:sn="10005" p:password="123123" p:name="孙红雷" p:department_sn="10003" p:post="员工">bean>
<bean id="employee2" class="cn.com.entity.Employee" p:sn="10005" p:password="123123" p:name="张鲁一" p:department_sn="10003" p:post="员工">bean>
web下创建Controller控制类
进入添加修改页面方法,增删改查方法
项目控制类
//员工表控制类
@Controller
@RequestMapping("/employee")
public class EmployeeController {
//注入Service层接口实现类
@Resource(name = "employeeService")
private EmployeeService employeeService;
//注入Service层部门接口实现类
@Resource(name = "departmentService")
private DepartmentService departmentService;
/* @RequestMapping(value = "/show")
public String show() {
System.out.println("进入Controller,show访问页面的映射,浏览器访问方法!");
return "redirect:/employee/getAllModel"; //JSP页面
}*/
//1.查询员工信息
//普通表单
//使用ModelAndView的,JSP
@RequestMapping(value = "/getAllModel")
public ModelAndView getAllModel(){
System.out.println("进入Controller,ModelAndView查询查询员工信息方法!");
//查询集合,调用Service层方法
List<Employee> employees = employeeService.getAll();
ModelAndView modelAndView = new ModelAndView();
//添加model数据
modelAndView.addObject("employees",employees);
//添加逻辑视图名,自动找页面
modelAndView.setViewName("employee_list");
System.out.println("封装完成! return modelAndView");
return modelAndView;
//进入JSP前端页面
}
//2.添加员工信息
//SpringMVC的form表单
//进入添加页面
@RequestMapping(value = "/to_add")
public String to_add(Map<String,Object> map){
System.out.println("进入Controller,to_add访问页面的映射,浏览器访问方法!");
map.put("employee",new Employee());
//封装部门信息
map.put("department",departmentService.getAll());
//添加静态常量类的数据
map.put("post", Contant.getPosts());
return "employee_add"; //JSP页面
}
//添加员工
@RequestMapping(value = "/insert")
public String add(Employee employee) {
System.out.println("进入Controller控制器,add方法,添加员工方法!");
//执行添加工单
employeeService.insert(employee);
//返回到下面输出页面方法
return "redirect:/employee/getAllModel";
}
//修改员工信息
//SpringMVC的form表单
//进入修改页面
@RequestMapping(value = "/to_update")
public String to_update(Map<String,Object> map, @RequestParam("sn") String sn){
System.out.println("进入Controller,to_update访问页面的映射,浏览器访问方法!");
map.put("employee",employeeService.getById(sn));
//添加部门信息
map.put("department",departmentService.getAll());
//添加静态常量类的数据
map.put("post", Contant.getPosts());
return "employee_update"; //JSP页面
}
//修改员工
@RequestMapping(value = "/update",params = "sn")
public String update(Employee employee) {
System.out.println("进入Controller控制器,update方法,编辑员工方法!");
//执行添加工单
employeeService.update(employee);
//返回到下面输出页面方法
return "redirect:/employee/getAllModel";
}
//删除员工信息
@RequestMapping(value = "/delete/{sn}")
public String delete(@PathVariable("sn") String sn) {
System.out.println("进入Controller控制器,delete方法,删除员工方法!");
//执行添加工单
employeeService.delete(sn);
//返回到下面输出页面方法
return "redirect:/employee/getAllModel";
}
}