maven构建ssm工程
2.1需求
在web工程的基础上实现SSM工程构建,实现对员工和部门的管理。
2.2数据库环境
创建数据库:maven
导入,maven.sql创建表
2.3定义pom.xml
maven工程首先要识别依赖,web工程实现SSM整合,需要依赖Spring、 springMVC、Mybatis等,在pom.xml添加工程如下依赖:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.igeek.maven</groupId> <artifactId>ssm</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging>
<!-- 添加工程的依赖 --> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- servlet-api JSP页面编译时需要的包 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> <scope>provided</scope> </dependency> <!-- Spring 以及 SpringMVC需要引入的包,自动引入需要参照的包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.6.RELEASE</version> </dependency> <!-- 持久层的包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.2</version> </dependency> <!-- Spring 和 Mybatis的整合包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- Mysql驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.19</version> </dependency> <!-- druid数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.28</version> </dependency>
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.10</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.6.RELEASE</version> </dependency> <!-- 打日志的 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.24</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.24</version> <scope>runtime</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> <scope>runtime</scope> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>ssm</finalName> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> <include>**/*.ini</include> </includes> </resource> </resources> <plugins> <!-- 设置编译版本为1.8 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin>
<!-- Jetty插件,提供一种web容器 --> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.4.2.v20170220</version> <configuration> <httpConnector> <!-- 配置运行的端口号 --> <port>80</port> </httpConnector> <!-- 配置扫描的时间间隔 --> <scanIntervalSeconds>1</scanIntervalSeconds> <webApp> <!-- 配置上下文 --> <contextPath>/ssm</contextPath> </webApp> </configuration> </plugin> </plugins> </build> </project> |
2.4dao
使用Mybatis作为持久层框架,可以使用Mybatis的逆向工程来生成我们需要的代码。
2.4.1domain模型类
在src/main/java创建模型类
部门实体
package com.igeekhome.ssm.domain; import java.io.Serializable; public class Department implements Serializable{ private Integer deptno; private String dname; private String loc; public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } public String getDname() { return dname; } public void setDname(String dname) { this.dname = dname == null ? null : dname.trim(); } public String getLoc() { return loc; } public void setLoc(String loc) { this.loc = loc == null ? null : loc.trim(); } } |
员工实体
package com.igeekhome.ssm.domain; import java.io.Serializable; import java.math.BigDecimal; import java.util.Date; public class Employee implements Serializable{ private Integer empno; private String ename; private String job; private Integer mgr; private Date hiredate; private BigDecimal sal; private BigDecimal comm; private Integer deptno; public Integer getEmpno() { return empno; } public void setEmpno(Integer empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename == null ? null : ename.trim(); } public String getJob() { return job; } public void setJob(String job) { this.job = job == null ? null : job.trim(); } public Integer getMgr() { return mgr; } public void setMgr(Integer mgr) { this.mgr = mgr; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } public BigDecimal getSal() { return sal; } public void setSal(BigDecimal sal) { this.sal = sal; } public BigDecimal getComm() { return comm; } public void setComm(BigDecimal comm) { this.comm = comm; } public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } } |
2.4.2Mapper配置文件
Department配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.igeekhome.ssm.dao.DepartmentMapper" > <resultMap id="BaseResultMap" type="com.igeekhome.ssm.domain.Department" > <id column="deptno" property="deptno" jdbcType="INTEGER" /> <result column="dname" property="dname" jdbcType="VARCHAR" /> <result column="loc" property="loc" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List" > deptno, dname, loc </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from dept where deptno = #{deptno,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from dept where deptno = #{deptno,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.igeekhome.ssm.domain.Department" > insert into dept (deptno, dname, loc ) values (#{deptno,jdbcType=INTEGER}, #{dname,jdbcType=VARCHAR}, #{loc,jdbcType=VARCHAR} ) </insert> <insert id="insertSelective" parameterType="com.igeekhome.ssm.domain.Department" > insert into dept <trim prefix="(" suffix=")" suffixOverrides="," > <if test="deptno != null" > deptno, </if> <if test="dname != null" > dname, </if> <if test="loc != null" > loc, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="deptno != null" > #{deptno,jdbcType=INTEGER}, </if> <if test="dname != null" > #{dname,jdbcType=VARCHAR}, </if> <if test="loc != null" > #{loc,jdbcType=VARCHAR}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.igeekhome.ssm.domain.Department" > update dept <set > <if test="dname != null" > dname = #{dname,jdbcType=VARCHAR}, </if> <if test="loc != null" > loc = #{loc,jdbcType=VARCHAR}, </if> </set> where deptno = #{deptno,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.igeekhome.ssm.domain.Department" > update dept set dname = #{dname,jdbcType=VARCHAR}, loc = #{loc,jdbcType=VARCHAR} where deptno = #{deptno,jdbcType=INTEGER} </update> </mapper> |
Employee配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.igeekhome.ssm.dao.EmployeeMapper" > <resultMap id="BaseResultMap" type="com.igeekhome.ssm.domain.Employee" > <id column="empno" property="empno" jdbcType="INTEGER" /> <result column="ename" property="ename" jdbcType="VARCHAR" /> <result column="job" property="job" jdbcType="VARCHAR" /> <result column="mgr" property="mgr" jdbcType="INTEGER" /> <result column="hiredate" property="hiredate" jdbcType="TIMESTAMP" /> <result column="sal" property="sal" jdbcType="DECIMAL" /> <result column="comm" property="comm" jdbcType="DECIMAL" /> <result column="deptno" property="deptno" jdbcType="INTEGER" /> </resultMap> <sql id="Base_Column_List" > empno, ename, job, mgr, hiredate, sal, comm, deptno </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from emp where empno = #{empno,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from emp where empno = #{empno,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.igeekhome.ssm.domain.Employee" > insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values (#{empno,jdbcType=INTEGER}, #{ename,jdbcType=VARCHAR}, #{job,jdbcType=VARCHAR}, #{mgr,jdbcType=INTEGER}, #{hiredate,jdbcType=TIMESTAMP}, #{sal,jdbcType=DECIMAL}, #{comm,jdbcType=DECIMAL}, #{deptno,jdbcType=INTEGER}) </insert> <insert id="insertSelective" parameterType="com.igeekhome.ssm.domain.Employee" > insert into emp <trim prefix="(" suffix=")" suffixOverrides="," > <if test="empno != null" > empno, </if> <if test="ename != null" > ename, </if> <if test="job != null" > job, </if> <if test="mgr != null" > mgr, </if> <if test="hiredate != null" > hiredate, </if> <if test="sal != null" > sal, </if> <if test="comm != null" > comm, </if> <if test="deptno != null" > deptno, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="empno != null" > #{empno,jdbcType=INTEGER}, </if> <if test="ename != null" > #{ename,jdbcType=VARCHAR}, </if> <if test="job != null" > #{job,jdbcType=VARCHAR}, </if> <if test="mgr != null" > #{mgr,jdbcType=INTEGER}, </if> <if test="hiredate != null" > #{hiredate,jdbcType=TIMESTAMP}, </if> <if test="sal != null" > #{sal,jdbcType=DECIMAL}, </if> <if test="comm != null" > #{comm,jdbcType=DECIMAL}, </if> <if test="deptno != null" > #{deptno,jdbcType=INTEGER}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.igeekhome.ssm.domain.Employee" > update emp <set > <if test="ename != null" > ename = #{ename,jdbcType=VARCHAR}, </if> <if test="job != null" > job = #{job,jdbcType=VARCHAR}, </if> <if test="mgr != null" > mgr = #{mgr,jdbcType=INTEGER}, </if> <if test="hiredate != null" > hiredate = #{hiredate,jdbcType=TIMESTAMP}, </if> <if test="sal != null" > sal = #{sal,jdbcType=DECIMAL}, </if> <if test="comm != null" > comm = #{comm,jdbcType=DECIMAL}, </if> <if test="deptno != null" > deptno = #{deptno,jdbcType=INTEGER}, </if> </set> where empno = #{empno,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.igeekhome.ssm.domain.Employee" > update emp set ename = #{ename,jdbcType=VARCHAR}, job = #{job,jdbcType=VARCHAR}, mgr = #{mgr,jdbcType=INTEGER}, hiredate = #{hiredate,jdbcType=TIMESTAMP}, sal = #{sal,jdbcType=DECIMAL}, comm = #{comm,jdbcType=DECIMAL}, deptno = #{deptno,jdbcType=INTEGER} where empno = #{empno,jdbcType=INTEGER} </update> </mapper> |
2.4.3Mapper接口
Department持久层接口
package com.igeekhome.ssm.dao; import com.igeekhome.ssm.domain.Department; public interface DepartmentMapper { int deleteByPrimaryKey(Integer deptno); int insert(Department record); int insertSelective(Department record); Department selectByPrimaryKey(Integer deptno); int updateByPrimaryKeySelective(Department record); int updateByPrimaryKey(Department record); } |
Employee持久层接口
package com.igeekhome.ssm.dao; import com.igeekhome.ssm.domain.Employee; public interface EmployeeMapper { int deleteByPrimaryKey(Integer empno); int insert(Employee record); int insertSelective(Employee record); Employee selectByPrimaryKey(Integer empno); int updateByPrimaryKeySelective(Employee record); int updateByPrimaryKey(Employee record); } |
2.4.4定义配置文件
在src/main/resources配置db.properties
driverClassName=com.mysql.jdbc.Driver jdbc_url=jdbc:mysql://localhost:3306/maven?useUnicode=true&characterEncoding=utf8 jdbc_username=root jdbc_password=root |
在src/main/resources配置log4j.properties
### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ### #在开发阶段日志级别使用debug log4j.rootLogger=debug, stdout ### 在日志中输出sql的输入参数 ### log4j.logger.org.hibernate.type=TRACE |
在 src/main/resources创建applicationContext.xml定义Spring的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!-- 自动扫描,排除扫描控制器,控制器交给SpringMVC进行扫描--> <context:component-scan base-package="com.igeekhome.ssm"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
<!-- 引入属性文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 配置数据源 --> <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc_url}" /> <property name="username" value="${jdbc_username}" /> <property name="password" value="${jdbc_password}" /> <!-- 初始化连接大小 --> <property name="initialSize" value="0" /> <!-- 连接池最大使用连接数量 --> <property name="maxActive" value="5" /> <!-- 连接池最大空闲 --> <property name="maxIdle" value="5" /> <!-- 连接池最小空闲 --> <property name="minIdle" value="0" /> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="60000" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="25200000" /> <!-- 打开removeAbandoned功能 --> <property name="removeAbandoned" value="true" /> <!-- 1800秒,也就是30分钟 --> <property name="removeAbandonedTimeout" value="1800" /> <!-- 关闭abanded连接时输出错误日志 --> <property name="logAbandoned" value="true" /> <!-- 监控数据库 --> <property name="filters" value="mergeStat" /> </bean>
<!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 拦截器方式配置事物 --> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="append*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="modify*" propagation="REQUIRED" /> <tx:method name="edit*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="repair" propagation="REQUIRED" /> <tx:method name="delAndRepair" propagation="REQUIRED" /> <tx:method name="get*" propagation="SUPPORTS" /> <tx:method name="find*" propagation="SUPPORTS" /> <tx:method name="load*" propagation="SUPPORTS" /> <tx:method name="search*" propagation="SUPPORTS" /> <tx:method name="datagrid*" propagation="SUPPORTS" /> <tx:method name="*" propagation="SUPPORTS" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* com.igeekhome.ssm.service..*Impl.*(..))" /> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" /> </aop:config>
<!-- 配置druid监控spring jdbc --> <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor"> </bean> <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype"> <property name="patterns"> <list> <value>com.igeekhome.ssm.service.*</value> </list> </property> </bean> <aop:config> <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" /> </aop:config> </beans> |
在 src/main/resources创建mybatis.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> </configuration> |
在 src/main/resources创建spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- myBatis文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis.xml" /> <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 --> <property name="mapperLocations" value="classpath:com/igeek/ssm/mapping/*.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.igeek.ssm.mapper" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> </beans>
|
2.4.5单元测试
在src/test/java创建单元测试类
测试Employee的Dao方法
package com.igeekhome.ssm.tests; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.igeekhome.ssm.dao.EmployeeMapper; import com.igeekhome.ssm.domain.Employee;
public class EmployeeTest {
@Test public void testFindEmployeeById(){ //加载配置文件 ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml", "spring-mybatis.xml"}); //获取dao EmployeeMapper employeeMapper = applicationContext.getBean(EmployeeMapper.class); //查询数据 Employee employee = employeeMapper.selectByPrimaryKey(7369); //查看数据 System.out.println(employee.getEname()); //关闭上下文 applicationContext.close(); } } |
测试Department的Dao方法
package com.igeek.test;
import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.igeek.ssm.mapper.DeptMapper; import com.igeek.ssm.pojo.Dept;
/** * @author www.igeehome.com * * TODO * */ public class DeptTest {
@Test public void testGetDept() { //加载spring配置文件 ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext( new String[] { "applicationContext.xml", "spring-mybatis.xml" }); //获取empMapper DeptMapper mapper = applicationContext.getBean(DeptMapper.class); //通过mapper查询 Dept dept = mapper.selectByPrimaryKey(20); System.out.println(dept); } } |
2.5Service
2.5.1定义Service接口
EmployeeService接口
package com.igeekhome.ssm.service;
import com.igeekhome.ssm.domain.Employee;
public interface EmployeeService { int deleteByPrimaryKey(Integer empno); int insert(Employee record); int insertSelective(Employee record); Employee selectByPrimaryKey(Integer empno); int updateByPrimaryKeySelective(Employee record);
int updateByPrimaryKey(Employee record); } |
DepartmentService接口
package com.igeekhome.ssm.service;
import com.igeekhome.ssm.domain.Department;
public interface DepartmentService { int deleteByPrimaryKey(Integer deptno); int insert(Department record); int insertSelective(Department record); Department selectByPrimaryKey(Integer deptno); int updateByPrimaryKeySelective(Department record); int updateByPrimaryKey(Department record); } |
2.5.2定义Service接口的实现类
EmployeeService接口实现类
package com.igeekhome.ssm.service.impl;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.igeekhome.ssm.dao.EmployeeMapper; import com.igeekhome.ssm.domain.Employee; import com.igeekhome.ssm.service.EmployeeService;
@Service public class EmployeeServiceImpl implements EmployeeService { @Autowired private EmployeeMapper employeeMapper; @Override public int deleteByPrimaryKey(Integer empno) { return employeeMapper.deleteByPrimaryKey(empno); } @Override public int insert(Employee record) { return employeeMapper.insert(record); } @Override public int insertSelective(Employee record) { return employeeMapper.insertSelective(record); } @Override public Employee selectByPrimaryKey(Integer empno) { return employeeMapper.selectByPrimaryKey(empno); } @Override public int updateByPrimaryKeySelective(Employee record) { return employeeMapper.updateByPrimaryKeySelective(record); } @Override public int updateByPrimaryKey(Employee record) { return employeeMapper.updateByPrimaryKey(record); } } |
DepartmentServiceImpl接口实现类
package com.igeekhome.ssm.service.impl;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.igeekhome.ssm.dao.DepartmentMapper; import com.igeekhome.ssm.domain.Department; import com.igeekhome.ssm.service.DepartmentService;
@Service public class DepartmentServiceImpl implements DepartmentService { @Autowired private DepartmentMapper departmentMapper; @Override public int deleteByPrimaryKey(Integer deptno) { return departmentMapper.deleteByPrimaryKey(deptno); } @Override public int insert(Department record) { return departmentMapper.insert(record); } @Override public int insertSelective(Department record) { return departmentMapper.insertSelective(record); } @Override public Department selectByPrimaryKey(Integer deptno) { return departmentMapper.selectByPrimaryKey(deptno); } @Override public int updateByPrimaryKeySelective(Department record) { return departmentMapper.updateByPrimaryKeySelective(record); } @Override public int updateByPrimaryKey(Department record) { return departmentMapper.updateByPrimaryKey(record); } } |
2.6Controller
2.6.1定义控制器
package com.igeekhome.ssm.controller;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod;
import com.igeekhome.ssm.domain.Employee; import com.igeekhome.ssm.service.EmployeeService;
@Controller public class EmployeeController { @Autowired private EmployeeService employeeService;
@RequestMapping(value="/emp",method=RequestMethod.GET) public String emp(){ return "employee"; }
@RequestMapping(value="/emp",method=RequestMethod.GET) public String findEmployeeByEmpno(int empno,ModelMap map){ Employee employee = employeeService.selectByPrimaryKey(empno); map.put("employee", employee); return "employee_info"; } } |
2.6.1定义SpringMVC配置文件
在src/main/resources配置spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 --> <context:component-scan base-package="com.igeekhome.ssm.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <property name="prefix" value="/WEB-INF/jsp"></property> <property name="suffix" value=".jsp"></property> </bean>
<mvc:default-servlet-handler/> <mvc:annotation-driven></mvc:annotation-driven> </beans>
|
定义web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
<servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml,classpath:spring-mybatis.xml</param-value> </context-param>
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<filter> <description>字符集过滤器</description> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <description>字符集编码</description> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> |
2.7Jsp
创建employee.jsp如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>员工查询</title> </head> <body> <form action="emp" method="get"> <input type="text" name="empno" placeholder="请输入员工编号"><input type="submit" value="查询"> </form> </body> </html> |
创建employee_info.jsp如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>员工信息</title> </head> <body> 员工编号:${employee.empno}<br> 员工姓名:${employee.ename}<br> 员工工作:${employee.job}<br> 员工薪资:${employee.sal}<br> 员工奖金:${employee.comm==null?0:employee.comm}<br> 员工入职日期:<fmt:formatDate value="${employee.hiredate}" pattern="yyyy-M-d" /><br> </body> </html> |