ssm与Maven(项目配置文件)
pom.xml(自动加载jar包) <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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Hello</groupId> <artifactId>scenicspot</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>scenicspot Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>utf8</project.build.sourceEncoding> <maven.compiler.encoding>utf8</maven.compiler.encoding> <spring.version>3.2.2.RELEASE</spring.version> <jackson.version>2.1.4</jackson.version> </properties> <dependencies> <!-- api自动生成 --> <dependency> <groupId>com.mangofactory</groupId> <artifactId>swagger-springmvc</artifactId> <version>1.0.2</version> </dependency> <!-- api自动生成 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> <exclusions> <!-- Exclude Commons Logging in favor of SLF4j --> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</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-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.0.4</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.5.6</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.10</version> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> <!-- 微信支付工具类 --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.0.1</version> </dependency> <!-- 银联支付安全 <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk16</artifactId> <version>1.46</version> </dependency>--> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!-- 数据库驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.25</version> </dependency> <!-- 整合mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.2</version> </dependency> <!-- 极光推送 --> <dependency> <groupId>cn.jpush.api</groupId> <artifactId>jpush-client</artifactId> <version>3.1.3</version> </dependency> <!-- pdf打印 <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.4.2</version> </dependency> <dependency> <groupId>com.itextpdf.tool</groupId> <artifactId>xmlworker</artifactId> <version>5.4.1</version> </dependency> --> <!-- 配置jacson --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.0.pr2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.0.pr2</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.0.pr2</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <!-- 日志 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency> <!-- excel导出 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.9</version> </dependency> <!-- xml解析 --> <dependency> <groupId>org.jdom</groupId> <artifactId>jdom</artifactId> <version>1.1.3</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <!-- 银联支付 <dependency> <groupId>com.unionpay</groupId> <artifactId>acp</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/upacp_sdk-1.0.0.jar</systemPath> </dependency>--> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>p6spy</groupId> <artifactId>p6spy</artifactId> <version>2.1.4</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- hibernate-validator --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.4.1.Final</version> </dependency> </dependencies> <build> <finalName>scenicspot0</finalName> </build> </project>
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> <!-- 数据库属性的默认值设置 --> <settings> <setting name="jdbcTypeForNull" value="NULL"></setting> </settings> <!-- 类的别名设置 --> <!-- <typeAliases> --> <!-- <typeAlias alias="Comment" type="com.qsy.scenicspot.entiy.Comment" /> <typeAlias alias="Notice" type="com.qsy.scenicspot.entiy.Notice"/> --> <!-- <typeAlias alias="Scenic" type="com.qsy.scenicspot.entiy.Scenic"/> --> <!-- <typeAlias alias = "Users" type="com.qsy.scenicspot.entiy.Users"/> --> <!-- </typeAliases> --> <!-- 将每一个mapper.xml配置文件与mybatis.xml进行关联 --><!--mapper resource="接口对应的xml(相当于实现类)"--> <!-- <mappers> <mapper resource="com.qsy.scenicspot.dao.AllScenicspotMapper.xml" /> </mappers> --> <mappers> <mapper resource = "com.qsy.scenicspot.dao.TestInterface.xml"></mapper> </mappers> </configuration>
applicationContext.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: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/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"> <context:annotation-config /> <context:component-scan base-package="com.qsy.scenicspot.controller" /> <context:component-scan base-package="com.qsy.scenicspot.dao" /> <context:component-scan base-package="com.qsy.scenicspot.service" /> <context:component-scan base-package="com.qsy.scenicspot.serviceImpl" /> <!-- 配置数据库的数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/scenicspot?characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <!-- 配置事务 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 使用注解完成数据库的打开关闭,即启动事务的注解操作 --> <tx:annotation-driven transaction-manager="txManager" /> <!-- 需要配置sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis.xml" /> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置baseMapper来完成如何获取mapper的基本任务 --> <bean id="baseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" abstract="true" lazy-init="true"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <!-- 配置dao层中需要的mapper --> <bean id="allScenicspotMapper" parent="baseMapper"><!-- <bean id="方法名" parent="连接MapperFactoryBean"><property name="mapperInterface" value="接口A位置">--> <property name="mapperInterface" value="com.qsy.scenicspot.dao.AllScenicspotMapper" /> </bean> </beans>
接口的xml
<?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"> <!-- namespace:绑定接口 --> <mapper namespace="com.qsy.scenicspot.dao.AllScenicspotMapper"><!--接口A位置--> <select id="getAll" resultType="com.qsy.scenicspot.entiy.Scenic"><!--select id="方法" resultType="返回值类型"--> select * from scenic<!--数据库语句--><!--#{0},#{1}有几个需要填写的依次写几个--> </select> </mapper>
MainAction.java
package com.qsy.scenicspot.controller; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.qsy.scenicspot.entiy.Scenic; import com.qsy.scenicspot.service.ServiceS; @Controller public class MainAction { //接口B注解 @Autowired ServiceS services;//接口声明变量(get,set方法) public ServiceS getServices() { return services; } public void setServices(ServiceS services) { this.services = services; } @RequestMapping("/goMains") public String goMain(HttpServletRequest request, HttpServletResponse response){ //String type = request.getParameter("type"); List<Scenic> scenics = null; scenics = services.getAll(); for (Scenic scenic : scenics) {
... }
return "login.htm"; } }
补充:for example
String result = "";
// 获取用户对象
Tuser tu = (Tuser) req.getSession().getAttribute("tu");
// 获取到选择的鲜花的ID集合
String strs = (String) req.getSession().getAttribute("strs");
// 获取到需要支付的总价
Double sum = (Double) req.getSession().getAttribute("sum");
// 获取到花的数量
Map<String, Integer> flowercount = (Map<String, Integer>) req.getSession().getAttribute("flowercount");
// 获取到花的总价
Map<String, Double> flowersum = (Map<String, Double>) req.getSession().getAttribute("flowersum");
try {
userService.buy(tu, strs, sum, flowercount, flowersum);
// req.getRequestDispatcher("MainServ").forward(req, resp);
result = "forward:goMainpost.do";//当跳转到另一个MainAction中的另一个方法时【@RequestMapping(value = "/goMainpost", method = RequestMethod.POST或者get)】
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
接口B:
package com.qsy.scenicspot.service; import java.util.List; import org.springframework.stereotype.Repository; import com.qsy.scenicspot.entiy.Scenic; @Repository public interface ServiceS { public List<Scenic> getAll(); }
实现类:
package com.qsy.scenicspot.serviceImpl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.qsy.scenicspot.dao.AllScenicspotMapper; import com.qsy.scenicspot.entiy.Scenic; import com.qsy.scenicspot.service.ServiceS; @Service("services") @Transactional(readOnly=true) public class ServiceImpl implements ServiceS{ @Autowired AllScenicspotMapper allScenicspotMapper; public AllScenicspotMapper getAllScenicspotMapper() { return allScenicspotMapper; } public void setAllScenicspotMapper(AllScenicspotMapper allScenicspotMapper) { this.allScenicspotMapper = allScenicspotMapper; } public List<Scenic> getAll() { List<Scenic> scenics=null; scenics=allScenicspotMapper.getAll(); System.out.println("1111"); return scenics; } }
接口A:
package com.qsy.scenicspot.dao; import java.util.List; import com.qsy.scenicspot.entiy.Scenic; @Repository public interface AllScenicspotMapper { public List<Scenic> getAll(); }