首先看看整个Demo的骨架:
首先这个demo整合是基于maven的,maven下载配置我就不说了,网上也有很多大神配置的案例,鼠标右键点击new选择maven project。在选择select an Archetype的时候选择webapp,通过myeclipse新建的maven项目只有一个src/main/resources包,所以还要自己手动新建几个src folder、详情请参照上图,接着就是要在pom.xml中添加dependencies,就是所需要的jar包。
<properties> <!-- spring版本号 --> <spring.version>3.2.4.RELEASE</spring.version> <!-- mybatis版本号 --> <mybatis.version>3.2.8</mybatis.version> <!-- log4j日志文件管理包版本 --> <slf4j.version>1.6.6</slf4j.version> <log4j.version>1.2.9</log4j.version> </properties> <dependencies> <!-- spring核心包 --> <!-- springframe start --> <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.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</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-jdbc</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-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- springframe end --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.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.2.2</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- mysql驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.29</version> </dependency> <!-- junit测试包 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- json数据 --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <!-- 日志文件管理包 --> <!-- log start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>asm</groupId> <artifactId>asm</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <!-- log end --> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.18.1-GA</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> <scope>provided</scope> </dependency> </dependencies>
jar包添加好后在src/main/resources/下面新建一个config包,用来装spring、springmvc,mybatis-config配置文件和各种properties文件。
1.配置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: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.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 加载外部的properties配置文件 --> <context:property-placeholder location="classpath:jdbc.properties" /> <context:component-scan base-package="com.gisquest"/> <!-- 配置数据库连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!-- 连接数据库驱动 --> <property name="driverClass" value="${driverClass}"></property> <!-- 数据库url --> <property name="jdbcUrl" value="${jdbcUrl}"></property> <!-- 数据库用户名 --> <property name="user" value="${user}"></property> <!-- 数据库密码--> <property name="password" value="${password}"></property> <!-- 其他配置--> <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <property name="initialPoolSize" value="3"></property> <!--连接池中保留的最小连接数。Default: 3 --> <property name="minPoolSize" value="3"></property> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="5"></property> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement" value="3"></property> <!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 --> <property name="maxStatements" value="8"></property> <!-- maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 --> <property name="maxStatementsPerConnection" value="5"></property> <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime" value="1800"></property> </bean> <!-- 配置SqlsessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:mapper/*Mapper.xml"/><!--这里没有用mybatis的配置文件所以在application中扫描Mapper文件,别名也在这配置--> <property name="typeAliasesPackage" value="com.gisquest.bean"></property> </bean> <!-- mapper扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开 --> <property name="basePackage" value="com.gisquest.dao"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
2.配置springmvc.xml
<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.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 激活spring注解方式:自动扫描,并注入bean --> <context:component-scan base-package="com.gisquest"/> <mvc:annotation-driven/> <!-- 视图解析器 解析jsp解析,默认使用jstl标签,classpath下的得有jstl的包 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 配置jsp路径的前缀 --> <property name="prefix" value="/WEB-INF/pages/"/> <!-- 配置jsp路径的后缀 --> <property name="suffix" value=".jsp"/> </bean> <!-- 文件上传 --> </beans>
因为只是一个demo没有配置文件上传。有需要的可以网上查找也可以联系我。
3.web.xml的配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1" metadata-complete="true"> <display-name>Archetype Created Web Application</display-name> <welcome-file-list> <welcome-file>/WEB-INF/pages/index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <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> <description>spring mvc 配置文件</description> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.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> <filter> <description>字符集过滤器</description> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <description>字符集编码</description> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
4.新建一个mapper包放在config包下,新建的mapper.xml都放到该包下,下面是UserMapper.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命名空间,作用就是对sql进行分类化管理,理解sql隔离 注意:使用mapper代理方法开发,namespace有特殊重要的作用,namespace等于mapper接口地址 --> <mapper namespace="com.gisquest.dao.UserMapper"> <!-- sql片段 --> <sql id="sql_where_dymatic"> <if test="username!=null"> AND username=#{username} </if> <if test="address!=null"> AND address=#{address} </if> </sql> <resultMap type="User" id="aliasResultMap"> <id column="id_" property="id"/> <result column="name_" property="username"/> <result column="address_" property="address"/> </resultMap> <!-- sql综合查询 --> <select id="findDynamic" resultType="User" parameterType="map"> SELECT * FROM user <where> <include refid="sql_where_dymatic"></include> </where> </select> <!-- 通过id查找 --> <select id="findByUserId" resultType="User" parameterType="Long"> SELECT * FROM user WHERE id=#{id} </select> <!-- 通过id查找,但是查找的列名是别名 --> <select id="findByAlias" resultType="String" parameterType="Long"> SELECT username name FROM user u WHERE u.id=#{id} </select> <!-- 通过resultMap输出 --> <select id="findByMap" resultMap="aliasResultMap" parameterType="Long"> SELECT id id_ ,username name_ ,address address_ FROM user u WHERE u.id=#{id} </select> <!-- 插入数据 --> <insert id="insertUser" parameterType="User"> <!-- 本来主键是自增的,在插入数据时还没有插入主键,所以将插入的主键返回到user对象中 --> <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long"> SELECT LAST_INSERT_ID() </selectKey> INSERT INTO user(username,sex,birthday,address) VALUE(#{username},#{sex},#{birthday},#{address}) </insert> <!-- 更新数据 --> <update id="updateUser" parameterType="User"> UPDATE user SET username=#{username},sex=#{sex},birthday=#{birthday},address=#{address} WHERE id=#{id} </update> <delete id="deleteUserById" parameterType="Long"> DELETE FROM user WHERE id=#{id} </delete> <resultMap type="User" id="UserOderResultMap"> <id column="id" property="id"/> <result column="username" property="username"/> <result column="sex" property="sex"/> <result column="birthday" property="birthday"/> <result column="address" property="address"/> <collection property="ordersList" ofType="Orders"> <id column="userId" property="userId"/> <result column="createtime" property="createtime"/> <result column="number" property="number"/> <result column="createtime" property="userId"/> <result column="note" property="note"/> </collection> </resultMap> <select id="findUserOrderMap" resultMap="UserOderResultMap"> select user.*,orders.number,orders.note,orders.createtime from orders, user where orders.userId=user.id </select> </mapper>
接下来是UserMapper.java 放在dao的package下:
package com.gisquest.dao; import java.util.List; import java.util.Map; import org.springframework.stereotype.Repository; import com.gisquest.bean.User; @Repository public interface UserMapper { public User findByUserId(Long id); public void insertUser(User user); //更新user public void updateUser(User user); //删除特定user public void deleteUserById(Long id); //使用sql片段动态查询 public List<User> findDynamic(Map paramMap); //使用别名查 public String findByAlias(Long id); public User findByMap(Long id); //userorders关联查询 public List<User> findUserOrderMap(); }
接着UserService
/** * @Project : test Maven Webapp * @Title : UserService.java * @Package com.gisquest.service * @Description : * @author laiyao * @Copyright : 2015 zhejiang shine Technology Co. Ltd All right reserved. * @date 2015年6月26日 下午12:40:45 * @version V1.0 */ package com.gisquest.service; import com.gisquest.bean.User; /** * @ClassName: UserService * @Description: * @author: laiyao * @date: 2015年6月26日下午12:40:45 */ public interface UserService { public User findByUserId(Long id); }
UserServiceImpl:
/** * @Project : test Maven Webapp * @Title : UserServiceImpl.java * @Package com.gisquest.service.Impl * @Description : * @author laiyao * @Copyright : 2015 zhejiang shine Technology Co. Ltd All right reserved. * @date 2015年6月26日 下午12:42:04 * @version V1.0 */ package com.gisquest.service.Impl; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.gisquest.bean.User; import com.gisquest.dao.UserMapper; import com.gisquest.service.UserService; /** * @ClassName: UserServiceImpl * @Description: * @author: laiyao * @date: 2015年6月26日下午12:42:04 */ @Service @Transactional public class UserServiceImpl implements UserService{ @Autowired private UserMapper userMapper; public void findByUserId1() { // TODO Auto-generated method stub User user=userMapper.findByUserId(1L); System.out.println(user); } /* (non-Javadoc) * @see com.gisquest.service.UserService#findByUserId(java.lang.Long) */ public User findByUserId(Long id) { // TODO Auto-generated method stub return userMapper.findByUserId(id); } }
最后controller:
/** * @Project : test Maven Webapp * @Title : UserController.java * @Package com.gisquest.controller * @Description : * @author laiyao * @Copyright : 2015 zhejiang shine Technology Co. Ltd All right reserved. * @date 2015年6月26日 下午1:43:29 * @version V1.0 */ package com.gisquest.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.gisquest.bean.User; import com.gisquest.service.UserService; /** * @ClassName: UserController * @Description: * @author: laiyao * @date: 2015年6月26日下午1:43:29 */ @Controller @RequestMapping("") public class UserController { @Autowired private UserService userService; @RequestMapping(value="/test.html",method=RequestMethod.POST) public String show(User user,Model model){ user=userService.findByUserId(1L); model.addAttribute("user", user); return"success"; } }
大功告成!
时间: 2024-11-07 12:35:46