您的位置:首页 > 编程语言 > Java开发

使用Eclipse搭建Maven项目(最近做个人网站,顺便记下来以防后面的项目要用)

2017-11-08 14:42 681 查看
1、打开Eclipse,File-New-Maven Project



2、点击下一步,如图所示选择webapp



3、点击下一步,如图自己填写



4、这样,我们就完成了创建Maven项目,架构如图所示:



5、接下来整合SSM框架,在pom.xml中添加jar包,在此文件中添加的包会自动下载到事先配置好的maven仓库:

代码如下:

<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>SSM</groupId>
<artifactId>PersonalWeb</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>TEST Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId>
<version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> -->
<properties>
<!-- spring版本号 -->
<spring.version>4.0.2.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.2.6</mybatis.version>
<!-- log4j日志文件管理包版本 -->
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<!-- 表示开发的时候引入,发布的时候不会加载此包 -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-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-aop</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
11a17
}</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>
<!-- 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>
<!-- 导入java ee jar 包 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency>
<!-- 导入Mysql数据库链接jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!-- JSTL标签类 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 日志文件管理包 -->
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>

<!-- 格式化对象,方便输出日志 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</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>
<!-- log end -->
<!-- 映入JSON -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- 上传组件包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>

</dependencies>
<build>
<finalName>PersonalWeb</finalName>
</build>
</project>


6、在src/main/resources下添加配置文件:spring-mybatis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 自动扫描 -->
<context:component-scan base-package="com.lzs.PersonalWeb" />
<!-- 引入配置文件 -->
<!-- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" /> </bean> -->

<context:property-placeholder
ignore-unresolvable="true" location="classpath:jdbc.properties" />
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${jdbc.initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${jdbc.maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${jdbc.minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${jdbc.maxWait}"></property>
</bean>

<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/lzs/PersonalWeb/dao/*.xml"></property>
</bean>

<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lzs.PersonalWeb.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>

<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

</beans>


7、配置数据库连接池jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/personalweb?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
#配置初始化大小、最小、最大
jdbc.initialSize=1
jdbc.minIdle=2
jdbc.maxIdle=20
jdbc.maxActive=20
#配置获取连接等待超时的时间
jdbc.maxWait=60000
#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
jdbc.timeBetweenEvictionRunsMillis=60000
#配置一个连接在池中最小生存的时间,单位是毫秒
jdbc.minEvictableIdleTimeMillis=300000
filters=stat,wall

8、配置日志:log4j.properties

log4j.rootLogger=INFO,Console,File

log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Encoding=utf-8
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} %C{1}@(%F:%L):%m%n

log4j.appender.File = org.apache.log4j.RollingFileAppender
log4j.appender.File.File = logs/ssm.log
log4j.appender.File.MaxFileSize = 10MB
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

#开发环境记录SQL日志
log4j.logger.com.eliteams=debug
log4j.logger.com.ibatis=debug
log4j.logger.java.sql.Statement=debug
log4j.logger.java.sql.PreparedStatement=debug,stdout


9、开始整合spring-mvc,配置spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<context:component-scan base-package="com.lzs.PersonalWeb.controller" />
<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->
</list>
</property>
</bean>
<!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
<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="defaultEncoding" value="utf-8" />
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="10485760000" />
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="40960" />
</bean>
</beans>

10、修改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" version="3.0">
<display-name>Archetype Created Web Application</display-name>
<!-- Spring和mybatis的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-mybatis.xml
</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<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>

<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

<!-- Spring MVC 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>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
<!-- 本人习惯使用加rest/前缀 -->
<!-- <url-pattern>*.do</url-pattern> -->
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<!-- 欢迎页面,访问/rest/index方法,也可以直接指定一个jsp(index.jsp) -->
<welcome-file>rest/index</welcome-file>
</welcome-file-list>
</web-app>


11、至此,maven整合ssm基本搭建完成,结构树如下:



此时,会发现项目中带红叉,查看problems中,显示如下错误:



经百度获得解决方法:
1、先改web.xml中webapp节点:
 
<?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">
 
然后关闭Eclipse,改项目下的.settings\org.eclipse.wst.common.project.facet.core.xml.将版本改成为3.1:<installed
facet="jst.web" version="3.1"/>,再启动Eclipse.
 
右键项目->Maven->Update Project
2、在pom.xml配置文件的<build></build>中加入如下语句:
<plugins>  
<plugin>  
<groupId>org.apache.maven.plugins</groupId>  
<artifactId>maven-compiler-plugin</artifactId>  
<configuration>  
<source>1.7(修改成自己JDK的版本)</source>  
<target>1.7(修改成自己JDK的版本)</target>  
</configuration>  
</plugin>  
</plugins>  
右键项目->Maven->Update Project

12、下面添加测试代码:在src/main/java下添加model层、service层、dao层、controller层:



自动生成dao层和model层:http://blog.csdn.net/qq_31874947/article/details/78392367
User.java:
package com.lzs.PersonalWeb.model;

import java.util.Date;

public class User {
private Integer id;

private String name;

private String ename;

private String sex;

private Double height;

private Date birth;

private String constellation;

private String hobby;

private String qq;

private String education;

private String bg;

private String phone;

private String email;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name == null ? null : name.trim();
}

public String getEname() {
return ename;
}

public void setEname(String ename) {
this.ename = ename == null ? null : ename.trim();
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex == null ? null : sex.trim();
}

public Double getHeight() {
return height;
}

public void setHeight(Double height) {
this.height = height;
}

public Date getBirth() {
return birth;
}

public void setBirth(Date birth) {
this.birth = birth;
}

public String getConstellation() {
return constellation;
}

public void setConstellation(String constellation) {
this.constellation = constellation == null ? null : constellation.trim();
}

public String getHobby() {
return hobby;
}

public void setHobby(String hobby) {
this.hobby = hobby == null ? null : hobby.trim();
}

public String getQq() {
return qq;
}

public void setQq(String qq) {
this.qq = qq == null ? null : qq.trim();
}

public String getEducation() {
return education;
}

public void setEducation(String education) {
this.education = education == null ? null : education.trim();
}

public String getBg() {
return bg;
}

public void setBg(String bg) {
this.bg = bg == null ? null : bg.trim();
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone == null ? null : phone.trim();
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email == null ? null : email.trim();
}
}

UserMapper.java

package com.lzs.PersonalWeb.dao;

import com.lzs.PersonalWeb.model.User;

public interface UserMapper {
int deleteByPrimaryKey(Integer id);

int insert(User record);

int insertSelective(User record);

User selectByPrimaryKey(Integer id);

int updateByPrimaryKeySelective(User record);

int updateByPrimaryKey(User record);
}
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" >
<mapper namespace="com.lzs.PersonalWeb.dao.UserMapper" >
<resultMap id="BaseResultMap" type="com.lzs.PersonalWeb.model.User" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="ename" property="ename" jdbcType="VARCHAR" />
<result column="sex" property="sex" jdbcType="VARCHAR" />
<result column="height" property="height" jdbcType="DOUBLE" />
<result column="birth" property="birth" jdbcType="DATE" />
<result column="constellation" property="constellation" jdbcType="VARCHAR" />
<result column="hobby" property="hobby" jdbcType="VARCHAR" />
<result column="QQ" property="qq" jdbcType="VARCHAR" />
<result column="education" property="education" jdbcType="VARCHAR" />
<result column="BG" property="bg" jdbcType="VARCHAR" />
<result column="phone" property="phone" jdbcType="VARCHAR" />
<result column="email" property="email" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
id, name, ename, sex, height, birth, constellation, hobby, QQ, education, BG, phone,
email
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from user
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from user
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.lzs.PersonalWeb.model.User" >
insert into user (id, name, ename,
sex, height, birth, constellation,
hobby, QQ, education,
BG, phone, email)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{ename,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR}, #{height,jdbcType=DOUBLE}, #{birth,jdbcType=DATE}, #{constellation,jdbcType=VARCHAR},
#{hobby,jdbcType=VARCHAR}, #{qq,jdbcType=VARCHAR}, #{education,jdbcType=VARCHAR},
#{bg,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.lzs.PersonalWeb.model.User" >
insert into user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="name != null" >
name,
</if>
<if test="ename != null" >
ename,
</if>
<if test="sex != null" >
sex,
</if>
<if test="height != null" >
height,
</if>
<if test="birth != null" >
birth,
</if>
<if test="constellation != null" >
constellation,
</if>
<if test="hobby != null" >
hobby,
</if>
<if test="qq != null" >
QQ,
</if>
<if test="education != null" >
education,
</if>
<if test="bg != null" >
BG,
</if>
<if test="phone != null" >
phone,
</if>
<if test="email != null" >
email,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
<if test="ename != null" >
#{ename,jdbcType=VARCHAR},
</if>
<if test="sex != null" >
#{sex,jdbcType=VARCHAR},
</if>
<if test="height != null" >
#{height,jdbcType=DOUBLE},
</if>
<if test="birth != null" >
#{birth,jdbcType=DATE},
</if>
<if test="constellation != null" >
#{constellation,jdbcType=VARCHAR},
</if>
<if test="hobby != null" >
#{hobby,jdbcType=VARCHAR},
</if>
<if test="qq != null" >
#{qq,jdbcType=VARCHAR},
</if>
<if test="education != null" >
#{education,jdbcType=VARCHAR},
</if>
<if test="bg != null" >
#{bg,jdbcType=VARCHAR},
</if>
<if test="phone != null" >
#{phone,jdbcType=VARCHAR},
</if>
<if test="email != null" >
#{email,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.lzs.PersonalWeb.model.User" >
update user
<set >
<if test="name != null" >
name = #{name,jdbcType=VARCHAR},
</if>
<if test="ename != null" >
ename = #{ename,jdbcType=VARCHAR},
</if>
<if test="sex != null" >
sex = #{sex,jdbcType=VARCHAR},
</if>
<if test="height != null" >
height = #{height,jdbcType=DOUBLE},
</if>
<if test="birth != null" >
birth = #{birth,jdbcType=DATE},
</if>
<if test="constellation != null" >
constellation = #{constellation,jdbcType=VARCHAR},
</if>
<if test="hobby != null" >
hobby = #{hobby,jdbcType=VARCHAR},
</if>
<if test="qq != null" >
QQ = #{qq,jdbcType=VARCHAR},
</if>
<if test="education != null" >
education = #{education,jdbcType=VARCHAR},
</if>
<if test="bg != null" >
BG = #{bg,jdbcType=VARCHAR},
</if>
<if test="phone != null" >
phone = #{phone,jdbcType=VARCHAR},
</if>
<if test="email != null" >
email = #{email,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.lzs.PersonalWeb.model.User" >
update user
set name = #{name,jdbcType=VARCHAR},
ename = #{ename,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR},
height = #{height,jdbcType=DOUBLE},
birth = #{birth,jdbcType=DATE},
constellation = #{constellation,jdbcType=VARCHAR},
hobby = #{hobby,jdbcType=VARCHAR},
QQ = #{qq,jdbcType=VARCHAR},
education = #{education,jdbcType=VARCHAR},
BG = #{bg,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
email = #{email,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>


UserService.java

package com.lzs.PersonalWeb.service;

import com.lzs.PersonalWeb.model.User;

public interface UserService {

int deleteByPrimaryKey(Integer id);

int insert(User record);

int insertSelective(User record);

User selectByPrimaryKey(Integer id);

int updateByPrimaryKeySelective(User record);

int updateByPrimaryKey(User record);

}


UserServiceImpl.java

package com.lzs.PersonalWeb.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lzs.PersonalWeb.dao.UserMapper;
import com.lzs.PersonalWeb.model.User;
import com.lzs.PersonalWeb.service.UserService;

@Service
public class UserServiceImpl implements UserService{

@Resource
UserMapper userMapper;

@Override
public int deleteByPrimaryKey(Integer id) {
return userMapper.deleteByPrimaryKey(id);
}

@Override
public int insert(User record) {
return userMapper.insert(record);
}

@Override
public int insertSelective(User record) {
return userMapper.insertSelective(record);
}

@Override
public User selectByPrimaryKey(Integer id) {
return userMapper.selectByPrimaryKey(id);
}

@Override
public int updateByPrimaryKeySelective(User record) {
return userMapper.updateByPrimaryKeySelective(record);
}

@Override
public int updateByPrimaryKey(User record) {
return userMapper.updateByPrimaryKey(record);
}

}


PageController.java

package com.lzs.PersonalWeb.controller;

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.RequestMapping;

import com.lzs.PersonalWeb.model.User;
import com.lzs.PersonalWeb.service.UserService;
import com.sun.tools.internal.ws.processor.model.Model;

@Controller
public class PageController {

@Resource
private UserService userService;

@RequestMapping("/index")
public String index(Model model) {
return "main";//spring-mvc.xml中自动添加头尾/WEB-INF/jsp/。。。.jsp
}

@RequestMapping("/welcome")
public String welcome(HttpServletRequest requert,HttpServletResponse response,
Model model) {
System.out.println("selectUser");

String id = requert.getParameter("id");
User user = userService.selectByPrimaryKey(Integer.valueOf(id));
return "welcome";//spring-mvc.xml中自动添加头尾/WEB-INF/jsp/。。。.jsp
}

}


main.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
hello world!你好世界!
<button onclick="toPage()">跳转到welcome页面</button>
</body>
<script>
function toPage() {
window.location.href = 'rest/welcome?id=1';
}
</script>
</html>


welcome.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=utf-8">
<title>test</title>
</head>
<body>
这是welcome页面
<input id="userName" value="${user.name}">
</body>
</html>


最终项目树:





(这个博客编辑器让我想死!)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: