Mybatis
1.加载jar包
- 创建po对象Xxx.java
- 创建映射文件Xxx.xml
- 创建资源文件jdbc.properties,日志文档,mybatis-config.xml(加载jdbc.properties,映射文件
- 使用dao层开发(需要写dao接口和dao实现类)
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL Map Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="cacheEnabled" value="true" /><!-- 全局映射器启用缓存 -->
<setting name="useGeneratedKeys" value="true" />
<setting name="defaultExecutorType" value="REUSE" />
</settings>
<typeAliases>
<!-- 这里添加生成的实体类 -->
<typeAlias type="com.interfacemanage.until.PageData" alias="pd"/>
<!-- 分页 -->
<typeAlias type="com.interfacemanage.until.Page" alias="Page"/>
</typeAliases>
</configuration>
Spring:
loc:控制反转即依赖注入,我认为其实loc是一种设计思想,而不是一种技术支持,在实际开发中因为每个类都有它要实现的功能,类与类之间要相互协作完成一些特定的功能,所以每个类要负责交互类的引用于依赖,这样就提升了代码的高耦合,则loc就是将依赖关系和引用交给spring来管理创建,这样其他类就可以只管自己要做的事了。就比如:病人生病了要买药,他去药店买药,自己看说明书是否对自己的病症,这样要看许多,但如果去医院看医生,医生可以针对他的病症直接给他给药,spring起的就是这个医生的作用。
aop:面向切面编程,我理解的是程序开发中有一种情况,类除了要完成特定的工作外还承担着额外的职责,比如事务,日志等。这些是贯穿整个程序的。则这时可以将事务独立出来,通过AOP我们使每一个承担事务的类一起组成一个切面,每当程序运行到某个切点时就将事务织入代理对象中。降低了耦合现象,简化了代码,使业务逻辑与横切关注点分离。
连接点:就是可以插入切面的一个点
切点:就是在何处切入一个或者多个点
切面:就是通知和切点共同的定义的全部内容
引入:允许当前类可以添加新的方法和属性
织入:把切面应用到切点,并创建代理对象
ApplicationContext-dataSource.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--1.引入外部资源文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<!--3. 配置sessionFactory -->
<!-- 配置mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- mapper扫描 -->
<property name="mapperLocations" value="classpath:mybatis/*.xml"></property>
</bean>
<!--2.配置 dataSource-->
<!-- 2.配置数据源 dataSource -->
<!-- 阿里 druid数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<!-- 数据库基本信息配置 -->
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<property name="driverClassName" value="${driverClassName}" />
<property name="filters" value="${filters}" />
<!-- 最大并发连接数 -->
<property name="maxActive" value="${maxActive}" />
<!-- 初始化连接数量 -->
<property name="initialSize" value="${initialSize}" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="${maxWait}" />
<!-- 最小空闲连接数 -->
<property name="minIdle" value="${minIdle}" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" />
<property name="validationQuery" value="${validationQuery}" />
<property name="testWhileIdle" value="${testWhileIdle}" />
<property name="testOnBorrow" value="${testOnBorrow}" />
<property name="testOnReturn" value="${testOnReturn}" />
<property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}" />
<!-- 打开removeAbandoned功能 数据库连接的自动回收机制-->
<property name="removeAbandoned" value="${removeAbandoned}" />
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" />
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="${logAbandoned}" />
</bean>
<!-- 4.sql会话模版 -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory" />
</bean>
<!--5.配置事物管理器 DataSourceTransactionManager-->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--6.配置事物增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="delete*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception"/>
<tx:method name="find*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
<tx:method name="update*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
<tx:method name="save*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
</tx:attributes>
</tx:advice>
<!--7.配置切入点aop -->
<!-- 事物处理 -->
<aop:config>
<aop:pointcut id="pc" expression="execution(* com.interfacemanage.services.*.*(..))" />
<aop:advisor pointcut-ref="pc" advice-ref="txAdvice" />
</aop:config>
</beans>
ApplicationContext-main.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 启用注解 -->
<context:annotation-config />
<!-- 启动组件扫描,排除@Controller组件,该组件由SpringMVC配置文件扫描,使用springmvc认为包下用了@controller注解的类是控制器 -->
<context:component-scan base-package="com.interfacemanage.controller">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
SpingMVC的工作原理
下面是文字步骤说明:
1、用户发送请求到前端控制器(DispatcherServlet)。
2、前端控制器请求处理器映射器(HandlerMapping)去查找处理器(Handler)。
3、找到以后处理器映射器(HandlerMappering)向前端控制器返回执行链(HandlerExecutionChain)。
4、前端控制器(DispatcherServlet)调用处理器适配器(HandlerAdapter)去执行处理器(Handler)。
5、处理器适配器去执行Handler。
6、处理器执行完给处理器适配器返回ModelAndView。
7、处理器适配器向前端控制器返回ModelAndView。
8、前端控制器请求视图解析器(ViewResolver)去进行视图解析。
9、视图解析器向前端控制器返回View。
10、前端控制器对视图进行渲染。
11、前端控制器向用户响应结果。
springmvc中通过HttpServletRequest获得前台传过来的参数。
通过ModelAndView中的addObject向前台传参,通过setViewNme方法指定界面
注:1.@RequestMapping定义controller方法对应的url
2.@ResponseBody作用:该注解用户将Controller的方法返回的对象,根据HTTP Request Header的Accept的内容,通过适当的HttpMessageConverter转换成指定格式后,写入到Reponse的body数据区。
多用于返回数据不是html标签的页面,而是其他格式的数据时(json,xml等)使用
3.@Resource通过name属性去匹配bean,当找不到时,按type去匹配,是J2EE的注解
4.@Autowired自动装配,默认按照type去匹配。主要消除java代码中的get与set以及bean中的property。是Spring的注解。
5.@Controller用来标注Controller,即控制层
6.@Service用来标注业务层
ApplicationContext-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--扩充了注解驱动,可以将请求参数绑定到控制器参数 -->
<mvc:annotation-driven/>
<!--在springMVC-servlet.xml中配置<mvc:default-servlet-handler />后,会在Spring MVC上下文中定义一个
org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler
,它会像一个检查员,对进入DispatcherServlet的URL进行筛查,如果发现是静态资源的请求,
就将该请求转由Web应用服务器默认的Servlet处理,如果不是静态资源的请求,才由DispatcherServlet继续处理。 -->
<mvc:default-servlet-handler/>
<!--1. 配置处理器Hander name 指定url -->
<!--自动扫描包,使用注解自动装配对象 -->
<context:component-scan base-package="com.interfacemanage" />
<!-- 对静态资源文件的访问 restful-->
<!-- <mvc:resources mapping="/admin/**" location="/,/admin/" />
<mvc:resources mapping="/static/**" location="/,/static/" />
<mvc:resources mapping="/plugins/**" location="/,/plugins/" />
<mvc:resources mapping="/uploadFiles/**" location="/,/uploadFiles/" /> -->
<!--2.配置处理器映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!--3.配置处理器适配器 HanderAdapter -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
<!-- 4.配置SpringMVC的视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<!-- 配置前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--配置后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
<!-- 上传拦截,如最大上传值及最小上传值 -->
<!-- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<property name="maxUploadSize">
<value>104857600</value>
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
<property name="defaultEncoding">
<value>utf-8</value>
</property>
</bean> -->
</beans>
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/ApplicationContext-dataSource.xml,
classpath:spring/ApplicationContext-main.xml,
classpath:spring/ApplicationContext-mvc.xml
</param-value>
</context-param>
<!--配置日志 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<!--编码过滤器 处理乱码 -->
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!--配置spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置springmvc -->
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/ApplicationContext-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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Maven
<dependencies>
<!-- spring start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-dao</artifactId>
<version>2.0.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
<version>2.0.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.18.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- spring end -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.james</groupId>
<artifactId>apache-mime4j</artifactId>
<version>0.6</version>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.4</version>
</dependency>
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxrpc</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>com.hynnet</groupId>
<artifactId>sqljdbc4-chs</artifactId>
<version>4.0.2206.100</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>7.0.47</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.3</version>
Mybatis
1.加载jar包
- 创建po对象Xxx.java
- 创建映射文件Xxx.xml
- 创建资源文件jdbc.properties,日志文档,mybatis-config.xml(加载jdbc.properties,映射文件
- 使用dao层开发(需要写dao接口和dao实现类)
5.1 dao接口
public interface ProductDao {
/**
* 根据ProductId查询商品
* @param ProductId
* @return
* @throws Exception
*/
public Product getProductById( int ProductId ) throws Exception;
/**
* 添加商品
* @param Product
* @throws Exception
*/
public void addProduct (Product Product )throws Exception;
/**
* 删除商品
* @param ProductId
* @throws Exception
*/
public void deleteProduct ( int ProductId )throws Exception;
}
5.2 dao接口实现类
public class ProductDaoImpl implements ProductDao {
// 需要向dao实现类中注入SqlSessionFactory
// 这里通过构造方法注入
private SqlSessionFactory sqlSessionFactory;
public ProductDaoImpl(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
@Override
public Product findProductById(int id) throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
Product Product = sqlSession.selectOne("com.oracle.po.Product.getProductById", id);
// 释放资源
sqlSession.close();
return Product;
}
@Override
public void insertProduct(Product Product) throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行插入操作
sqlSession.insert("com.oracle.po.Product.addProduct", Product);
// 提交事务
sqlSession.commit();
// 释放资源
sqlSession.close();
}
@Override
public void deleteProduct(int id) throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行插入操作
sqlSession.delete("com.oracle.po.Product.deleteProduct", id);
// 提交事务
sqlSession.commit();
// 释放资源
sqlSession.close();
}
}
Spring:
loc:控制反转即依赖注入,我认为其实loc是一种设计思想,而不是一种技术支持,在实际开发中因为每个类都有它要实现的功能,类与类之间要相互协作完成一些特定的功能,所以每个类要负责交互类的引用于依赖,这样就提升了代码的高耦合,则loc就是将依赖关系和引用交给spring来管理创建,这样其他类就可以只管自己要做的事了。就比如:病人生病了要买药,他去药店买药,自己看说明书是否对自己的病症,这样要看许多,但如果去医院看医生,医生可以针对他的病症直接给他给药,spring起的就是这个医生的作用。
aop:面向切面编程,我理解的是程序开发中有一种情况,类除了要完成特定的工作外还承担着额外的职责,比如事务,日志等。这些是贯穿整个程序的。则这时可以将事务独立出来,通过AOP我们使每一个承担事务的类一起组成一个切面,每当程序运行到某个切点时就将事务织入代理对象中。降低了耦合现象,简化了代码,使业务逻辑与横切关注点分离。
连接点:就是可以插入切面的一个点
切点:就是在何处切入一个或者多个点
切面:就是通知和切点共同的定义的全部内容
引入:允许当前类可以添加新的方法和属性
织入:把切面应用到切点,并创建代理对象
ApplicationContext-dataSource.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--1.引入外部资源文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<!--3. 配置sessionFactory -->
<!-- 配置mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- mapper扫描 -->
<property name="mapperLocations" value="classpath:mybatis/*.xml"></property>
</bean>
<!--2.配置 dataSource-->
<!-- 2.配置数据源 dataSource -->
<!-- 阿里 druid数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<!-- 数据库基本信息配置 -->
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<property name="driverClassName" value="${driverClassName}" />
<property name="filters" value="${filters}" />
<!-- 最大并发连接数 -->
<property name="maxActive" value="${maxActive}" />
<!-- 初始化连接数量 -->
<property name="initialSize" value="${initialSize}" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="${maxWait}" />
<!-- 最小空闲连接数 -->
<property name="minIdle" value="${minIdle}" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" />
<property name="validationQuery" value="${validationQuery}" />
<property name="testWhileIdle" value="${testWhileIdle}" />
<property name="testOnBorrow" value="${testOnBorrow}" />
<property name="testOnReturn" value="${testOnReturn}" />
<property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}" />
<!-- 打开removeAbandoned功能 数据库连接的自动回收机制-->
<property name="removeAbandoned" value="${removeAbandoned}" />
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" />
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="${logAbandoned}" />
</bean>
<!-- 4.sql会话模版 -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory" />
</bean>
<!--5.配置事物管理器 DataSourceTransactionManager-->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--6.配置事物增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="delete*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception"/>
<tx:method name="find*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
<tx:method name="update*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
<tx:method name="save*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
</tx:attributes>
</tx:advice>
<!--7.配置切入点aop -->
<!-- 事物处理 -->
<aop:config>
<aop:pointcut id="pc" expression="execution(* com.interfacemanage.services.*.*(..))" />
<aop:advisor pointcut-ref="pc" advice-ref="txAdvice" />
</aop:config>
</beans>
ApplicationContext-main.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 启用注解 -->
<context:annotation-config />
<!-- 启动组件扫描,排除@Controller组件,该组件由SpringMVC配置文件扫描,使用springmvc认为包下用了@controller注解的类是控制器 -->
<context:component-scan base-package="com.interfacemanage.controller">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
SpingMVC的工作原理
下面是文字步骤说明:
1、用户发送请求到前端控制器(DispatcherServlet)。
2、前端控制器请求处理器映射器(HandlerMapping)去查找处理器(Handler)。
3、找到以后处理器映射器(HandlerMappering)向前端控制器返回执行链(HandlerExecutionChain)。
4、前端控制器(DispatcherServlet)调用处理器适配器(HandlerAdapter)去执行处理器(Handler)。
5、处理器适配器去执行Handler。
6、处理器执行完给处理器适配器返回ModelAndView。
7、处理器适配器向前端控制器返回ModelAndView。
8、前端控制器请求视图解析器(ViewResolver)去进行视图解析。
9、视图解析器向前端控制器返回View。
10、前端控制器对视图进行渲染。
11、前端控制器向用户响应结果。
springmvc中通过HttpServletRequest获得前台传过来的参数。
通过ModelAndView中的addObject向前台传参,通过setViewNme方法指定界面
注:1.@RequestMapping定义controller方法对应的url
2.@ResponseBody作用:该注解用户将Controller的方法返回的对象,根据HTTP Request Header的Accept的内容,通过适当的HttpMessageConverter转换成指定格式后,写入到Reponse的body数据区。
多用于返回数据不是html标签的页面,而是其他格式的数据时(json,xml等)使用
3.@Resource通过name属性去匹配bean,当找不到时,按type去匹配,是J2EE的注解
4.@Autowired自动装配,默认按照type去匹配。主要消除java代码中的get与set以及bean中的property。是Spring的注解。
5.@Controller用来标注Controller,即控制层
6.@Service用来标注业务层
ApplicationContext-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--扩充了注解驱动,可以将请求参数绑定到控制器参数 -->
<mvc:annotation-driven/>
<!--在springMVC-servlet.xml中配置<mvc:default-servlet-handler />后,会在Spring MVC上下文中定义一个
org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler
,它会像一个检查员,对进入DispatcherServlet的URL进行筛查,如果发现是静态资源的请求,
就将该请求转由Web应用服务器默认的Servlet处理,如果不是静态资源的请求,才由DispatcherServlet继续处理。 -->
<mvc:default-servlet-handler/>
<!--1. 配置处理器Hander name 指定url -->
<!--自动扫描包,使用注解自动装配对象 -->
<context:component-scan base-package="com.interfacemanage" />
<!-- 对静态资源文件的访问 restful-->
<!-- <mvc:resources mapping="/admin/**" location="/,/admin/" />
<mvc:resources mapping="/static/**" location="/,/static/" />
<mvc:resources mapping="/plugins/**" location="/,/plugins/" />
<mvc:resources mapping="/uploadFiles/**" location="/,/uploadFiles/" /> -->
<!--2.配置处理器映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!--3.配置处理器适配器 HanderAdapter -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
<!-- 4.配置SpringMVC的视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<!-- 配置前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--配置后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
<!-- 上传拦截,如最大上传值及最小上传值 -->
<!-- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<property name="maxUploadSize">
<value>104857600</value>
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
<property name="defaultEncoding">
<value>utf-8</value>
</property>
</bean> -->
</beans>
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/ApplicationContext-dataSource.xml,
classpath:spring/ApplicationContext-main.xml,
classpath:spring/ApplicationContext-mvc.xml
</param-value>
</context-param>
<!--配置日志 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<!--编码过滤器 处理乱码 -->
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!--配置spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置springmvc -->
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/ApplicationContext-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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Maven
<dependencies>
<!-- spring start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-dao</artifactId>
<version>2.0.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
<version>2.0.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.18.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- spring end -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.james</groupId>
<artifactId>apache-mime4j</artifactId>
<version>0.6</version>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.4</version>
</dependency>
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxrpc</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>com.hynnet</groupId>
<artifactId>sqljdbc4-chs</artifactId>
<version>4.0.2206.100</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>7.0.47</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
Dao.java
package com.interfacemanage.dao;
import com.interfacemanage.until.PageData;
/**
* 基础dao接口
* @author A0026805
*
* @param <E>
*/
public interface Dao{
/**
* 保存对象
* @param str
* @param obj
* @return
* @throws Exception
*/
public Object save(String str, Object obj) throws Exception;
/**
* 修改对象
* @param str
* @param obj
* @return
* @throws Exception
*/
public Object update(String str, Object obj) throws Exception;
/**
* 删除对象
* @param str
* @param obj
* @return
* @throws Exception
*/
public Object delete(String str, Object obj) throws Exception;
/**
* 查找对象
* @param str
* @param obj
* @return
* @throws Exception
*/
public Object findForObject(String str, Object obj) throws Exception;
/**
* 查找对象
* @param str
* @param obj
* @return
* @throws Exception
*/
public Object findForList(String str, Object obj) throws Exception;
/**
* 查找对象封装成Map
* @param s
* @param obj
* @return
* @throws Exception
*/
public Object findForMap(String sql, Object obj, String key , String value) throws Exception;
}
SupportDao.java
package com.interfacemanage.dao;
import java.util.List;
import javax.annotation.Resource;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;
/**
* 基础Dao实现类
* @author A0026805
*
* @param <E>
*/
@Repository("daoSupport")
public class SupportDao implements Dao{
@Resource(name = "sqlSessionTemplate")
//sqlSessionTemplate实现了SqlSession接口,也就是说我们可以使用SqlSessionTemplate来代理以往的DefailtSqlSession完成对数据库的操作
protected SqlSessionTemplate sqlSessionTemplate;
/**
* 保存对象
* @param str
* @param obj
* @return
* @throws Exception
*/
public Object save(String str, Object obj) throws Exception {
return sqlSessionTemplate.insert(str, obj);
}
/**
* 批量更新
* @param str
* @param obj
* @return
* @throws Exception
*/
public Object batchSave(String str, List objs )throws Exception{
return sqlSessionTemplate.insert(str, objs);
}
/**
* 修改对象
* @param str
* @param obj
* @return
* @throws Exception
*/
public Object update(String str, Object obj) throws Exception {
return sqlSessionTemplate.update(str, obj);
}
/**
* 批量更新
* @param str
* @param obj
* @return
* @throws Exception
*/
public void batchUpdate(String str, List objs )throws Exception{
SqlSessionFactory sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory();
//批量执行器
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false);
try{
if(objs!=null){
for(int i=0,size=objs.size();i<size;i++){
sqlSession.update(str, objs.get(i));
}
sqlSession.flushStatements();
sqlSession.commit();
sqlSession.clearCache();
}
}finally{
sqlSession.close();
}
}
/**
* 批量更新
* @param str
* @param obj
* @return
* @throws Exception
*/
public Object batchDelete(String str, List objs )throws Exception{
return sqlSessionTemplate.delete(str, objs);
}
/**
* 删除对象
* @param str
* @param obj
* @return
* @throws Exception
*/
public Object delete(String str, Object obj) throws Exception {
return sqlSessionTemplate.delete(str, obj);
}
/**
* 查找对象
* @param str
* @param obj
* @return
* @throws Exception
*/
public Object findForObject(String str, Object obj) throws Exception {
return sqlSessionTemplate.selectOne(str, obj);
}
/**
* 查找对象
* @param str
* @param obj
* @return
* @throws Exception
*/
public Object findForList(String str, Object obj) throws Exception {
return sqlSessionTemplate.selectList(str, obj);
}
public Object findForMap(String str, Object obj, String key, String value) throws Exception {
return sqlSessionTemplate.selectMap(str, obj, key);
}
}
ApiMessageServices.java
package com.interfacemanage.services;
import java.util.List;
import com.interfacemanage.until.Page;
import com.interfacemanage.until.PageData;
/**
* API信息 services层
* @author A0026805
*
*/
public interface ApiMessageServices {
/**
* 列表 API信息管理
* @param page
* @return
* @throws Exception
*/
public List<PageData> list(PageData pd) throws Exception;
/**删除
* @param pd
* @throws Exception
*/
public void delete(PageData pd)throws Exception;
/**批量删除
* @param ArrayDATA_IDS
* @throws Exception
*/
public void deleteAll(String[] ArrayDATA_IDS)throws Exception;
/**通过id获取数据
* @param pd
* @throws Exception
*/
public PageData findById(PageData pd)throws Exception;
/**修改
* @param pd
* @throws Exception
*/
public void edit(PageData pd)throws Exception;
/**新增
* @param pd
* @throws Exception
*/
public void save(PageData pd)throws Exception;
/**关联删除
* @param pd
* @throws Exception
*/
public void deleteCorrelation( PageData pd)throws Exception;
/**查找一个API信息
* @param CATEGORY_ID
* @throws Exception
*/
public PageData selectApiMessage(String CATEGORY_ID) throws Exception;
/**列表(全部)
* @param pd
* @throws Exception
*/
public List<PageData> listAll(PageData pd)throws Exception;
/**查找API信息数量
*
*/
public PageData selectApiMessageCount(PageData pd)throws Exception;
/**查找一页API信息
*
*/
public List<PageData> selectApiMessagePage(PageData pd)throws Exception;
public List<PageData> search(PageData pd)throws Exception;
}
ApiMessageServicesImple.java
package com.interfacemanage.servicesimple;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.interfacemanage.dao.SupportDao;
import com.interfacemanage.services.ApiMessageServices;
import com.interfacemanage.until.Page;
import com.interfacemanage.until.PageData;
/**
* API信息 services实现层
* @author A0026805
*
*/
@Service("ApiMessageServices")
public class ApiMessageServicesImple implements ApiMessageServices{
@Resource(name="daoSupport")
private SupportDao dao;
@SuppressWarnings("unchecked")
public List<PageData> list(PageData pd) throws Exception {
return (List<PageData>)dao.findForList("ApiMessageMapper.datalistPage", pd);
}
public void delete(PageData pd) throws Exception {
dao.delete("ApiMessageMapper.deleteOneData", pd);
}
public void deleteAll(String[] ArrayDATA_IDS) throws Exception {
dao.delete("ApiMessageMapper。deleteAll", ArrayDATA_IDS);
}
public PageData findById(PageData pd) throws Exception {
return (PageData)dao.findForObject("ApiMessageMapper.findbyId", pd);
}
public void edit(PageData pd) throws Exception {
dao.update("ApiMessageMapper.edit", pd);
}
public void save(PageData pd) throws Exception {
dao.save("ApiMessageMapper.save", pd);
}
public PageData selectApiMessage(String CATEGORY_ID) throws Exception {
return (PageData)dao.findForObject("ApiMessageMapper.findbyId", CATEGORY_ID);
}
@SuppressWarnings("unchecked")
public List<PageData> listAll(PageData pd) throws Exception {
return (List<PageData>)dao.findForList("ApiMessageMapper.listAll", pd);
}
public void deleteCorrelation(PageData pd) {
// TODO Auto-generated method stub
}
public PageData selectApiMessageCount(PageData pd) throws Exception {
return (PageData)dao.findForObject("ApiMessageMapper.selectCount", pd);
}
@SuppressWarnings("unchecked")
public List<PageData> selectApiMessagePage(PageData pd) throws Exception {
return (List<PageData>)dao.findForList("ApiMessageMapper.currentCount", pd);
}
@SuppressWarnings("unchecked")
public List<PageData> search(PageData pd)throws Exception{
return (List<PageData>)dao.findForList("ApiMessageMapper.search", pd);
}
}
ApiMessageController.java
package com.interfacemanage.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.interfacemanage.services.ApiMessageServices;
import com.interfacemanage.until.BaseController;
import com.interfacemanage.until.GetUUID;
import com.interfacemanage.until.JudgeUserState;
import com.interfacemanage.until.Page;
import com.interfacemanage.until.PageData;
import com.interfacemanage.until.UserState;
/**
* API信息Controller
* @author A0026805
*
*/
@Controller
@RequestMapping("/apiMessage")
public class ApiMessageController extends BaseController{
@Resource(name="ApiMessageServices")
private ApiMessageServices apiMessageServices;
/**列表(第一次检索一页的Api信息)
*
*
* @return
* @throws Exception
*/
@RequestMapping(value="/firstPagelist")
public ModelAndView findPageList() throws Exception{
PageData pd=new PageData();
pd=this.getPageData();
int firstNumber=1;
int endNumber=1;
ModelAndView mv=new ModelAndView();
Page page=new Page();
pd.put("pageData",page.getPageData() );
pd.put("currentPage", page.getCurrentPage());
firstNumber=((Integer)pd.get("currentPage")-1)*(Integer)pd.get("pageData")+1;
endNumber=(Integer)pd.get("currentPage")*(Integer)pd.get("pageData");
pd.put("firstNumber", firstNumber);
pd.put("endNumber", endNumber);
pd.put("pageData", 5);
List<PageData> varList=apiMessageServices.selectApiMessagePage(pd);
for(int i=0;i<varList.size();i++){
Double e=(Double)varList.get(i).get("a_Number");
int api_Number=e.intValue();
varList.get(i).put("api_Number", api_Number);
}
HttpSession session=this.getRequest().getSession();
PageData pageData=(PageData)session.getAttribute("pd");
JudgeUserState userState=new JudgeUserState();
UserState QX=userState.judgeState(pageData);
mv.setViewName("apimssage/apimessagelist");
mv.addObject("varList", varList);
mv.addObject("pd", pd);
mv.addObject("QX", QX);
return mv;
}
@RequestMapping(value="/skipSearch")
public ModelAndView skipSearch() throws Exception{
ModelAndView mv=new ModelAndView();
mv.setViewName("apimssage/apimessageSearch");
mv.addObject("judge",1);
return mv;
}
@RequestMapping(value="/search")
public ModelAndView search() throws Exception{
PageData pd=new PageData();
pd=this.getPageData();
String key=(String)pd.get("keywords");
String keywords=key.replaceAll(" ", "");
pd.put("keywords", keywords);
ModelAndView mv=new ModelAndView();
List<PageData> varList=apiMessageServices.search(pd);
if(!varList.isEmpty()&&varList!=null){
for(int i=0;i<varList.size();i++){
Double e=(Double)varList.get(i).get("a_Number");
int api_Number=e.intValue();
varList.get(i).put("api_Number", api_Number);
}
}
HttpSession session=this.getRequest().getSession();
PageData pageData=(PageData)session.getAttribute("pd");
JudgeUserState userState=new JudgeUserState();
UserState QX=userState.judgeState(pageData);
mv.setViewName("apimssage/apimessageSearch");
mv.addObject("varList", varList);
mv.addObject("pd", pd);
mv.addObject("QX", QX);
mv.addObject("judge",0);
return mv;
}
/**列表(检索一页的Api信息)
*
*
* @return
* @throws Exception
*/
@RequestMapping(value="/Pagelist")
public ModelAndView findPageVarList() throws Exception{
PageData pd=new PageData();
pd=this.getPageData();
int firstNumber=1;
int endNumber=1;
int pageDatafirst=Integer.valueOf((String)pd.get("pageData"));
int currentPagefirst=Integer.valueOf((String)pd.get("currentPage"));
ModelAndView mv=new ModelAndView();
List<PageData> list=apiMessageServices.listAll(pd);
long endPage=1;
PageData pageDa=apiMessageServices.selectApiMessageCount(pd);
long api_Count=(Long)pageDa.get("api_Count");
if(api_Count%Long.valueOf((String)pd.get("pageData"))!=0){
endPage=api_Count/Long.valueOf((String)pd.get("pageData"))+1;
}else{
endPage=api_Count/Long.valueOf((String)pd.get("pageData"));
}
if((currentPagefirst==1||currentPagefirst<=0)&¤tPagefirst!=-1){//首页
firstNumber=1;
endNumber=1*pageDatafirst;
pd.put("currentPage", 1);
}else{
if(currentPagefirst==-1||currentPagefirst>=endPage){
endNumber=list.size();
firstNumber=(int) ((endPage-1)*pageDatafirst)+1;
pd.put("currentPage",endPage );
}else{
firstNumber=(currentPagefirst-1)*pageDatafirst+1;
endNumber=currentPagefirst*pageDatafirst;
pd.put("currentPage",currentPagefirst );
}
}
pd.put("firstNumber", firstNumber);
pd.put("endNumber", endNumber);
pd.put("pageData", pageDatafirst);
List<PageData> varList=apiMessageServices.selectApiMessagePage(pd);
for(int i=0;i<varList.size();i++){
Double e=(Double)varList.get(i).get("a_Number");
int api_Number=e.intValue();
varList.get(i).put("api_Number", api_Number);
}
HttpSession session=this.getRequest().getSession();
PageData pageData=(PageData)session.getAttribute("pd");
JudgeUserState userState=new JudgeUserState();
UserState QX=userState.judgeState(pageData);
mv.setViewName("apimssage/apimessagelist");
mv.addObject("varList", varList);
mv.addObject("pd", pd);
mv.addObject("QX", QX);
return mv;
}
/**
* 判断pageData与currentPage是否为空
*/
@RequestMapping(value="/judgePage" ,produces="application/json;charset=UTF-8")
@ResponseBody
public Object judgePage() throws Exception{
PageData pd=this.getPageData();
Map<String, String> data=new HashMap<String, String>();
if(pd.get("currentPage")!=null&&pd.get("pageData")!=null){
data.put("currentPage", (String)pd.get("currentPage"));
data.put("pageData", (String)pd.get("pageData"));
data.put("result", "success");
}
return data;
}
/**根据ID删除一条api信息
*
*
* @return
* @throws Exception
*/
@RequestMapping(value="/delete" ,produces="application/json;charset=UTF-8")
@ResponseBody
public Object deleteErrorcodeById() throws Exception{
Map<String, String> data=new HashMap<String, String>();
PageData pd=new PageData();
pd=this.getPageData();
String pageData=(String)pd.get("pageData");
String currentPage=(String)pd.get("currentPage");
apiMessageServices.delete(pd);
data.put("result", "success");
data.put("pageData", pageData);
data.put("currentPage", currentPage);
return data;
}
/**
* 去修改界面
* @return
* @throws Exception
*/
@RequestMapping(value="/goEdit")
public ModelAndView goEdit() throws Exception{
PageData pd=new PageData();
pd=this.getPageData();
PageData pageData=apiMessageServices.findById(pd);
ModelAndView mv=new ModelAndView();
mv.addObject("pd", pageData);
mv.setViewName("apimssage/apimessageedit");
mv.addObject("msg", "edit");
return mv;
}
/**修改api信息
*
* @return
* @throws Exception
*/
@RequestMapping(value="/edit")
public ModelAndView editErrorcode() throws Exception{
ModelAndView mv=new ModelAndView();
PageData pd=new PageData();
pd=this.getPageData();
apiMessageServices.edit(pd);
mv.addObject("msg", "success");
mv.setViewName("save_result");
return mv;
}
/**
* 去添加界面
*/
@RequestMapping(value="/goAdd")
public ModelAndView goAdd()throws Exception{
PageData pd=new PageData();
pd=this.getPageData();
ModelAndView mv=new ModelAndView();
mv.setViewName("apimssage/apimessageedit");
mv.addObject("msg", "add");
return mv;
}
/**
* 添加
*/
@RequestMapping(value="/add")
public ModelAndView Add()throws Exception{
PageData pd=new PageData();
pd=this.getPageData();
ModelAndView mv=new ModelAndView();
String APIMESSAGE_ID=GetUUID.getUUID();
pd.put("APIMESSAGE_ID", APIMESSAGE_ID);
apiMessageServices.save(pd);
mv.addObject("msg", "success");
mv.setViewName("save_result");
return mv;
}
}