SSM框架整合 IDEA_Maven
首先是配置web的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- 字符集过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/conf/spring.xml</param-value>
</context-param>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/conf/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list> <error-page>
<error-code>404</error-code>
<location>/page/error-page/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/page/error-page/500.jsp</location>
</error-page>
<error-page>
<error-code>405</error-code>
<location>/page/error-page/405.jsp</location>
</error-page> </web-app>
把配置文件的路径改成自己的对应路径
然后修改maven的pom.xml
<repositories>
<repository>
<id>maven - ali</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>4.3.4.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.4.1</mybatis.version>
</properties> <dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency> <dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency> <!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency> <!-- 实现slf4j接口并整合 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency> <!-- JSON -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.3</version>
</dependency> <!-- 数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
<scope>runtime</scope>
</dependency> <!-- 数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency> <!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency> <!-- mybatis/spring整合包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency> <!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 实现 jsp 页面的复用 -->
<dependency>
<groupId>com.googlecode.rapid-framework</groupId>
<artifactId>rapid-core</artifactId>
<version>4.0.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
<!-- jstl -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- jstl-api -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<!-- jstl-impl -->
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jstl-impl</artifactId>
<version>1.2</version>
</dependency> <!-- standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency> <!-- jsf-api -->
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency> <!-- jsf-api -->
<!-- https://mvnrepository.com/artifact/javax.faces/jsf-impl -->
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>1.2_12</version>
</dependency> <!-- commons系列包 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency> <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.8.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency> <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<!-- haspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl -->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
</dependency> <!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency> <!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20190722</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-spec</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
</dependency> </dependencies>
注入相应的maven依赖,需要其他包的可以去maven仓库查找
然后是配置文件
spring.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <!-- 扫描service、dao组件 -->
<context:component-scan base-package="com.spring" />
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/conf/jdbc.properties" />
</bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}"></property>
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property> <!-- 分页功能配置 -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<!--使用下面的方式配置参数,一行配置一个 -->
<value>
params=value1
</value>
</property>
</bean>
</array>
</property>
<!-- 分页功能配置结束 -->
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.spring.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<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.spring.service..*Impl.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut"
advice-ref="transactionAdvice" />
</aop:config> </beans>
具体可以看注释
修改 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:p="http://www.springframework.org/schema/p"
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"> <!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />
<mvc:resources location="/images/" mapping="/images/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:default-servlet-handler /> <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<context:component-scan
base-package="com.spring.controller" />
<!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<!-- <property name="prefix" value="/WEB-INF/jsp/" /> -->
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--上传文件的最大大小,单位为字节 -->
<property name="maxUploadSize" value="17367648787"></property> <!-- 上传文件的编码 -->
<property name="defaultEncoding" value="UTF-8"></property>
</bean> <!--控制器拦截-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:mapping path="/index.html"/>
<mvc:exclude-mapping path="/css/**"/>
<mvc:exclude-mapping path="/fonts/**"/>
<mvc:exclude-mapping path="/images/**"/>
<mvc:exclude-mapping path="/js/**"/>
<mvc:exclude-mapping path="/teacher/login"/> <bean class="com.spring.Handler.SpringMvcHandler"></bean> </mvc:interceptor>
</mvc:interceptors>
</beans>
修改spring-mybatits.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>
<!-- plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下: properties?, settings?, typeAliases?,
typeHandlers?, objectFactory?,objectWrapperFactory?, plugins?, environments?,
databaseIdProvider?, mappers? -->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
<property name="param1" value="value1" />
</plugin>
</plugins> </configuration>
最后是jdbc.properties
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmDB?characterEncoding=utf-8&serverTimezone=UTC&allowMultiQueries=true
jdbc.username=root
jdbc.password=123456
initialSize=10
maxActive=10
maxIdle=10
minIdle=0
maxWait=5
bean层 dao层都可以用逆向工具生成
先看一下项目结构
讲解一下dao层代码
package com.spring.dao; import java.util.List;
import java.util.Map; import com.spring.bean.DeptBean; public interface DeptBeanMapper {
int deleteByPrimaryKey(String dno); int insert(DeptBean record); int insertSelective(DeptBean record); DeptBean selectByPrimaryKey(String dno); int updateByPrimaryKeySelective(DeptBean record); int updateByPrimaryKey(DeptBean record); public List<DeptBean> list(DeptBean dept);
}
dao层是接口,方法名对应mapper。xml里面查询语句的id
<?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.spring.dao.DeptBeanMapper">
<resultMap id="BaseResultMap" type="com.spring.bean.DeptBean"><!--查询语句返回值注入实体类,字段对应-->
<id column="dno" jdbcType="VARCHAR" property="dno"/>
<result column="name" jdbcType="VARCHAR" property="name"/>
<result column="principal" jdbcType="VARCHAR" property="principal"/>
<result column="mobile" jdbcType="VARCHAR" property="mobile"/>
<result column="remark" jdbcType="VARCHAR" property="remark"/>
</resultMap>
<sql id="Base_Column_List">
dno, name, principal, mobile, remark
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap"><!--parameterType 是传入的参数类型 ResultType是返回值类型-->
select
<include refid="Base_Column_List"/>
from dept where dno = #{dno,jdbcType=VARCHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String"> delete
from dept
where dno = #{dno,jdbcType=VARCHAR} </delete>
<insert id="insert"
parameterType="com.spring.bean.DeptBean"> insert into dept (dno, name, principal, mobile, remark)
values (#{dno,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR},
#{principal,jdbcType=VARCHAR}, #{mobile,jdbcType=VARCHAR},
#{remark,jdbcType=VARCHAR}) </insert>
<insert id="insertSelective" parameterType="com.spring.bean.DeptBean">insert into dept
<trim prefix="(" suffix=")" suffixOverrides=","><!--suffix是后缀添加suffixOverrides是去除后缀,另外两个是添加前缀,去除前缀-->
<if test="dno != null">dno,</if>
<if test="name != null">name,</if>
<if test="principal != null">principal,</if>
<if test="mobile != null">mobile,</if>
<if test="remark != null">remark,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="dno != null">#{dno,jdbcType=VARCHAR},</if>
<if test="name != null">#{name,jdbcType=VARCHAR},</if>
<if test="principal != null">#{principal,jdbcType=VARCHAR},</if>
<if test="mobile != null">#{mobile,jdbcType=VARCHAR},</if>
<if test="remark != null">#{remark,jdbcType=VARCHAR},</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.spring.bean.DeptBean">update dept
<set>
<if test="name != null">name = #{name,jdbcType=VARCHAR},</if>
<if test="principal != null">principal = #{principal,jdbcType=VARCHAR},</if>
<if test="mobile != null">mobile = #{mobile,jdbcType=VARCHAR},</if>
<if test="remark != null">remark = #{remark,jdbcType=VARCHAR},</if>
</set>
where dno = #{dno,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="com.spring.bean.DeptBean"> update dept
set name = #{name,jdbcType=VARCHAR},
principal = #{principal,jdbcType=VARCHAR},
mobile = #{mobile,jdbcType=VARCHAR},
remark = #{remark,jdbcType=VARCHAR}
where dno = #{dno,jdbcType=VARCHAR} </update>
<select id="list" parameterType="com.spring.bean.DeptBean" resultMap="BaseResultMap">select
<include refid="Base_Column_List"/>
from dept
<trim prefix="where" prefixOverrides="OR|AND">
<if test="dno != null">dno = #{dno,jdbcType=VARCHAR}</if>
<if test="name != null">and name = #{name,jdbcType=VARCHAR}</if>
<if test="principal != null">and principal = #{principal,jdbcType=VARCHAR}</if>
<if test="mobile != null">and mobile = #{mobile,jdbcType=VARCHAR}</if>
<if test="remark != null">remark = #{remark,jdbcType=VARCHAR}</if>
</trim>
</select>
</mapper>
Service 层
package com.spring.service; import java.util.ArrayList;
import java.util.List;
import java.util.Map; import javax.annotation.Resource; import com.spring.bean.TeacherBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.spring.bean.DeptBean;
import com.spring.dao.DeptBeanMapper;
import com.spring.service.core.BaseService; @Service("deptService")//service层的注解 spring的注解式编程
public class DeptService implements BaseService<DeptBean> { @Autowired//这个是自动装配,是spring里面的,按照类型自动装配@Resourcs是jdk自带的,按照名字装配
DeptBeanMapper deptDao;
@Override//遵从接口,接口自己定义的
public List<DeptBean> list(DeptBean t) {
List<DeptBean> list = null;
try {
list = deptDao.list(t);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return list;
} @Override
public DeptBean find(String dno) {
// TODO Auto-generated method stub
DeptBean deptBean = null;
try {
deptBean = deptDao.selectByPrimaryKey(dno);
} catch (Exception e) {
// TODO: handle exception
}
return deptBean;
} @Override
public int save(DeptBean t) {
// TODO Auto-generated method stub
int i=0;
try {
deptDao.insertSelective(t);
i=1;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
i=0;
}
return i;
} @Override
public int update(DeptBean t) {
// TODO Auto-generated method stub
int i=0;
try {
deptDao.updateByPrimaryKeySelective(t);
i=1;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
i=0;
}
return i;
} @Override
public int delete(String dno) {
// TODO Auto-generated method stub
int i=0;
try {
deptDao.deleteByPrimaryKey(dno);
i=1;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
i=0;
}
return i;
} @Override
public TeacherBean loginTeacher(TeacherBean record) {
return null;
} }
最后是控制层 Controller
1 package com.spring.controller;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.List;
6
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9
10 import com.spring.bean.TeacherBean;
11 import org.springframework.beans.factory.annotation.Autowired;
12 import org.springframework.stereotype.Controller;
13 import org.springframework.web.bind.annotation.RequestMapping;
14 import org.springframework.web.bind.annotation.ResponseBody;
15 import org.springframework.web.servlet.ModelAndView;
16
17 import com.spring.bean.DeptBean;
18 import com.spring.service.core.BaseService;
19
20 import net.sf.json.JSONArray;
21 import net.sf.json.JSONObject;
22
23 @Controller//控制层注解
24 @RequestMapping("/dept")//请求地址访问这个类里面的方法都要在前面加一个dept
25 public class DeptController {
26
27 @Autowired
28 BaseService<DeptBean> deptService;
29
30 @Autowired
31 public BaseService<TeacherBean> teacherService;
32
33 @RequestMapping(value="/listjson",produces = "application/json;charset=utf-8")
34 @ResponseBody//加了这注解返回值就不是一个页面了可以是json,map list 等
35 public String listjson() {
36 List<DeptBean> list =deptService.list(null);
37 JSONArray jsonArray = new JSONArray();
38 for (int i = 0; i < list.size(); i++) {
39 JSONObject jsonObject = new JSONObject();
40 jsonObject.put("dno", list.get(i).getDno());
41 jsonObject.put("name", list.get(i).getName());
42 jsonArray.add(jsonObject);
43 }
44 JSONObject jsonObject = new JSONObject();
45 jsonObject.put("data", jsonArray);
46 return jsonObject.toString();
47 }
48 ModelAndView mav=null;
49 @RequestMapping("/list")
50 public ModelAndView list(HttpServletRequest request){
51
52 String tno = (String) request.getSession().getAttribute("tno");
53
54 TeacherBean teacher = teacherService.find(tno);
55 DeptBean deptBean = new DeptBean();
56 deptBean.setDno(teacher.getDno());
57 if(!teacher.getType().equals("0")) {
58 List<DeptBean> listofdept = deptService.list(null);
59 mav = new ModelAndView("page/deptInfo/deptlist2");
60 mav.addObject("listofdept", listofdept);
61 }else{
62 List<DeptBean> listofdept = deptService.list(null);
63 mav = new ModelAndView("page/deptInfo/deptlist");
64 mav.addObject("listofdept", listofdept);
65 }
66
67 return mav;
68 }
69 @RequestMapping("/save")
70 public void adddept(DeptBean dept,HttpServletResponse response) throws IOException{
71 response.setContentType("text/html;charset=gb2312");
72 PrintWriter out = response.getWriter();
73 if (deptService.save(dept)==0) {
74 out.print("<script language=\"javascript\">dialog('保存失败','请检查数据');window.location.href='list'</script>");
75 }else {
76 response.sendRedirect("list");
77 }
78 // deptService.save(dept);
79 // response.sendRedirect("list");
80 }
81 @RequestMapping("/delete")
82 public void deldept(String dno,HttpServletResponse response) throws IOException{
83
84 response.setContentType("text/html;charset=gb2312");
85 PrintWriter out = response.getWriter();
86 if (deptService.delete(dno)==0) {
87 out.print("<script language=\"javascript\">alert('删除失败');window.location.href='list'</script>");
88 }else {
89 response.sendRedirect("list");
90 }
91
92 }
93 @RequestMapping("/update")
94 public void updept(DeptBean dept,HttpServletResponse response) throws IOException{
95 response.setContentType("text/html;charset=gb2312");
96 PrintWriter out = response.getWriter();
97 if (deptService.update(dept)==0) {
98 out.print("<script language=\"javascript\">alert('修改失败,请检查输入');window.location.href='list'</script>");
99 }else {
100 response.sendRedirect("list");
101 }
102 // response.sendRedirect("list");
103 }
104 }
SSM框架整合 IDEA_Maven的更多相关文章
- SSM框架整合项目 :租房管理系统
使用ssm框架整合,oracle数据库 框架: Spring SpringMVC MyBatis 导包: 1, spring 2, MyBatis 3, mybatis-spring 4, fastj ...
- 基于maven的ssm框架整合
基于maven的ssm框架整合 第一步:通过maven建立一个web项目. 第二步:pom文件导入jar包 (1 ...
- JavaWeb之ssm框架整合,用户角色权限管理
SSM框架整合 Spring SpringMVC MyBatis 导包: 1, spring 2, MyBatis 3, mybatis-spring 4, fastjson 5, aspectwea ...
- SSM框架整合环境构建——基于Spring4和Mybatis3
目录 环境 配置说明 所需jar包 配置db.properties 配置log4j.properties 配置spring.xml 配置mybatis-spring.xml 配置springmvc.x ...
- springmvc(二) ssm框架整合的各种配置
ssm:springmvc.spring.mybatis这三个框架的整合,有耐心一步步走. --WH 一.SSM框架整合 1.1.整合思路 从底层整合起,也就是先整合mybatis与spring,然后 ...
- SSM框架整合的其它方式
---------------------siwuxie095 SSM 框架整合的其它方式 1.主要是整合 Spring ...
- SSM框架整合过程总结
-----------------------siwuxie095 SSM 框架整合过程总结 1.导入相关 jar 包( ...
- SSM框架整合思想
-------------------siwuxie095 SSM 框架整合思想 1.SSM 框架,即 SpringMV ...
- SSM框架整合搭建教程
自己配置了一个SSM框架,打算做个小网站,这里把SSM的配置流程详细的写了出来,方便很少接触这个框架的朋友使用,文中各个资源均免费提供! 一. 创建web项目(eclipse) File-->n ...
随机推荐
- 算法-搜索(6)B树
B树是平衡的m路搜索树. 根结点至少两个子女,根结点以外的非失败结点至少⌈m/2⌉个子女,所有失败结点都在h+1层. 第h层至少2⌈m/2⌉h-1个结点,因此失败结点数n+1≥2⌈m/2⌉h-1个. ...
- 报错:ER_NO_DEFAULT_FOR_FIELD: Field 'status' doesn't have a default value
小白入门级错误,数据库插入数据时报错;ER_NO_DEFAULT_FOR_FIELD: Field 'status' doesn't have a default value 百度说是my.ini文 ...
- Combine 框架,从0到1 —— 2.通过 ConnectablePublisher 控制何时发布
本文首发于 Ficow Shen's Blog,原文地址: Combine 框架,从0到1 -- 2.通过 ConnectablePublisher 控制何时发布. 内容概览 前言 使用 ma ...
- 超简单!基于Python搭建个人“云盘”
1. 简介 当我们想要从本地向云服务器上传文件时,比较常用的有pscp等工具,但避免不了每次上传都要写若干重复的代码,而笔者最近发现的一个基于Python的工具updog,可以帮助我们在服务器上搭建类 ...
- 记录Unity的优化tip(不断更新)
大概记录遇到的可以优化的点.1.Mesh.UploadMeshData:预先把网格送到GPU unity是这样的,它对一个网格,先把它搞到内存,然后在第一次渲染它时把它送到GPU.但送GPU经常是个瓶 ...
- 关于前端Ajaxc传FormData后台如何接收转base64
前端是Jquery的ajax,后台是C#MVC,代码如下: <------前端-----> var formData = new FormData(); formData.append(& ...
- Easy Problem(等差数列求和导公式)
链接:https://ac.nowcoder.com/acm/contest/316/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言2621 ...
- 《闲扯Redis十一》Redis 有序集合对象底层实现
一.前言 Redis 提供了5种数据类型:String(字符串).Hash(哈希).List(列表).Set(集合).Zset(有序集合),理解每种数据类型的特点对于redis的开发和运维非常重要. ...
- 安装JDK的攻略
打开浏览器搜索JDK官网 单击Oracle下的JDK Download. 如果英语不好就直接打开浏览器自带翻译功能,根据自己的系统以及相应版本,下载对应的JDK 根据安装提示进行安装,路径自己选择. ...
- leetcode刷题-61旋转链表
题目 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2输出: 4 ...