一、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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zp</groupId> <artifactId>test2</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <!-- 依赖servlet,导入servlet所需jar包 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <!-- 依赖spring,导入spring所需jar包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>1.9.2.RELEASE</version> </dependency> <!-- URL重写filter,用于将访问静态资源http://localhost/foo.css 转为http://localhost/static/foo.css --> <dependency> <groupId>org.tuckey</groupId> <artifactId>urlrewritefilter</artifactId> <version>4.0.4</version> </dependency> <!-- json的jar包 --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.11</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.12</version> </dependency> <!-- 数据库连接所需jar包 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.6</version> </dependency> <!-- 阿里巴巴公司开发的Druid数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.16</version> </dependency> <!-- mybatis核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.1.1</version> </dependency> <!-- mybatis/spring包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <!-- Mysql驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.27</version> </dependency> </dependencies> <build> <plugins> <!-- 声明jdk编译版本1.7 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <!-- tomcat插件 --> <plugin> <groupId>org.apche.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> </plugins> </build> </project>
二、aplicationContext.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:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" 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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.9.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <bean id="dbcp" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="username" value="root"> </property> <property name="password" value="123"> </property> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://localhost:3306/OrderDatabase?characterEncoding=utf8"> </property> <!-- 连接初始值,连接池启动时创建的连接数量的初始值 --> <property name="initialSize"><value>4</value></property> <!-- 连接池的最大值,同一时间可以从池分配的最多连接数量,0时无限制 --> <property name="maxActive"><value>100</value></property> <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 ,0时无限制--> <property name="maxIdle"><value>50</value></property> <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> <property name="minIdle"><value>15</value></property> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait"><value>10000</value></property><!-- 最大等待时间 --> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis"><value>60000</value></property> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis"><value>300000</value></property> </bean> <!-- 设置sqlsession工厂 --> <bean id="sqlsessionfactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dbcp"> </property> <property name="mapperLocations" value="classpath:com/zp/test/sql/*.xml"> </property> </bean> <!-- DAO层 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.zp.test.dao"> </property> </bean> <!-- viewresolver 视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"> </property> <property name="suffix" value=".jsp"> </property> </bean> <!-- 扫描service,controller --> <context:component-scan base-package="com.zp.test"/> <!-- handlermapping @RequestMapping, @ResponseBody,@ExceptionHanlder --> <mvc:annotation-driven/> <!-- Spring事务管理 --> <!-- 定义事务管理(共通处理)组件 --> <bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 必须注入dataSource连接资源 --> <property name="dataSource" ref="dbcp"> </property> </bean> <!-- 启动@Transactional注解,将来哪个组件方法 带有该标记就会切入tx事务管理功能 --> <tx:annotation-driven transaction-manager="tx"/> </beans>
三、web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="schedule-console" version="3.0"> <!-- 配置spring的servlet --> <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/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 覆盖default servlet的/, springmvc servlet将处理原来处理静态资源的映射 --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 该servlet为tomcat,jetty等容器提供,将静态资源映射从/改为/static/目录,如原来访问 http://localhost/foo.css ,现在http://localhost/static/foo.css --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/static/*</url-pattern> </servlet-mapping> <!-- URL重写filter,用于将访问静态资源http://localhost/foo.css 转为http://localhost/static/foo.css --> <filter> <filter-name>UrlRewriteFilter</filter-name> <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> <init-param> <param-name>confReloadCheckInterval</param-name> <param-value>60</param-value> </init-param> <init-param> <param-name>logLevel</param-name> <param-value>DEBUG</param-value> </init-param> </filter> <!-- 浏览器不支持put,delete等method,由该filter将/blog?_method=delete转换为标准的http delete方法 --> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <servlet-name>springmvc</servlet-name> </filter-mapping> <!-- Spring的log4j监听器 --> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- 字符集 过滤器 --> <filter> <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 欢迎页 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 设置session有效时间 --> <session-config> <session-timeout>9999</session-timeout> </session-config> </web-app>
四、建立本地数据库
库名:OrderDatabase
表名:user
字段
添加一条数据
五、编写代码
1、项目结构
在src/main/java下新建com.zp.test包
2、entity包下编写实体类
package com.zp.test.entity; public class User { private int user_id; private String user_name; private String password; private String phoneNumber; private String image_address; private String email; private String province; private String city; private int user_status; private int limit; private int isdel; private String erweima; public int getUser_id() { return user_id; } public void setUser_id(int user_id) { this.user_id = user_id; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public String getImage_address() { return image_address; } public void setImage_address(String image_address) { this.image_address = image_address; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public int getUser_status() { return user_status; } public void setUser_status(int user_status) { this.user_status = user_status; } public int getLimit() { return limit; } public void setLimit(int limit) { this.limit = limit; } public int getIsdel() { return isdel; } public void setIsdel(int isdel) { this.isdel = isdel; } public String getErweima() { return erweima; } public void setErweima(String erweima) { this.erweima = erweima; } }
3、sql包下编写sql语句
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.zp.test.dao.UserDao"> <select id="selectUser" resultType="com.zp.test.entity.User" parameterType="map"> SELECT * FROM user where user_id = #{user_id} </select> </mapper>
4、dao包下编写Dao
package com.zp.test.dao; import java.util.List; import java.util.Map; import com.zp.test.entity.User; public interface UserDao { public List<User> selectUser(Map<String,Object> map); }
5、service包下编写service
package com.zp.test.service; import java.util.List; import java.util.Map; import com.zp.test.entity.User; public interface UserService { public List<User> selectUser(Map<String,Object> map); }
6、serviceImpl包下编写serviceImpl
package com.zp.test.service.serviceImpl; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.zp.test.dao.UserDao; import com.zp.test.entity.User; import com.zp.test.service.UserService; @Service public class UserServiceImpl implements UserService{ @Resource private UserDao ud; @Override public List<User> selectUser(Map<String, Object> map) { return ud.selectUser(map); } }
7、controller包下编写controller
package com.zp.test.controller; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.zp.test.entity.User; import com.zp.test.service.UserService; @Controller @RequestMapping("/userinfo") public class UserController { @Resource private UserService us; @RequestMapping(value="/{id}") @ResponseBody public List<User> test(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response){ System.out.println(id); Map<String,Object> paramMap=new HashMap<String,Object>(); paramMap.put("user_id", id); List<User> ulist = us.selectUser(paramMap); for(User u : ulist){ System.out.println(u.getUser_name()); } return ulist; } }
8、编写index.jsp页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <script type="text/javascript" src="static/js/jquery-1.10.1.min.js"></script> <script> function test1(){ $.ajax({ url:"/test2/userinfo/5", data:{}, type:"post", dataType:"json", success:function(data){ alert("success"); alert(data[0].user_name); }, error:function(){ alert("error"); } }); } function test2(){ window.location.href="test2.jsp"; } </script> <body> <input type="button" value="test1" onclick="test1();"/> <input type="button" value="test2" onclick="test2();"/> </body> </html>
六、运行项目,进入欢迎页,点击test1
返回查询结果,与步骤四中数据库表中数据一致,证明搭建成功
代码上传至github
地址:https://github.com/Neonuu/test2