IDEA搭建SSM项目并实现简单案例,步骤超详细
前言
最近手头上没啥活儿,就想用IDEA搭建一个SSM项目,结果发现不百度根本搭不出来,就算是搭建好了也是有很多问题,为了以后方便上手,在此一步一步记录一下。
一、搭建SSM框架
【File】-->【New】-->【Project】
选择左侧【Maven】,在【Create from archetype】前面打【√】,选择【org.apache.maven.archetype:maven-archetype-webapp】,点击【Next】
初始状态下【Name】为untitled,修改【Name】,下面的【Location和【ArtifactId】会自动填写为一样的,如果不自动生成就手动填写吧。
【GroupId】默认值为org.example,需要手动修改,当然也可以不改,后期修改配置文件注意配置的路径就可以。
填写完毕,点击【Next】。
配置maven,选择本地maven地址,在【Override】前面打上【√】,才可以选择maven中的setting.xml文件以及本地仓库,然后点击【Finish】IDEA就开始创建项目了
在这里多提一句,我用的是阿里云的maven镜像,下载jar包的速度挺快,至于怎么配置请移步这里 Maven配置阿里云镜像
创建之后,项目会自动为你加载jar包,如果没有jar的情况下,如果在上面那步配置使用阿里云maven镜像,加载就会快一些,要不然就会慢多了。
二、完善SSM框架结构
创建之后项目的结构是这样的,很显然,这不是我们需要的标准SSM框架
我们新建包,为项目补充缺失的包
选中项目的src,右键选择【New】-->【Directory】
弹窗有四个选项,我们先添加【main\java】,之后再按照相同的步骤,把剩下的三个加上。
加上之后的效果就是这样的
接下来新建一下项目所需要的文件包继续补充项目结构。
右键项目中的java文件夹,点击【New】-->【Package】
先建一个跟刚开始新建项目的时候,GroupId相同值的包
然后在com.manage包下依次加上controller(web层)、dao、entry(实体类)、service(接口类),
在service下新建impl包用来存放接口的实现类,
在resource下增加mapper用来存放映射的xml文件。
在webapp下新加一个page文件夹用来存放jsp页面,并将index.jsp挪到该文件夹下。加上之后如下
三、编写配置文件
首先在pom.xml中引入spring以及mybatis以及mysql等的相关依赖,为方便参考,我直接把代码粘到下面:
<?xml version="1.0" encoding="UTF-8"?> <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.manage</groupId> <artifactId>resourcemanage</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>resourcemanage Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <!--spring版本--> <spring.version>5.0.5.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- 实现slf4j接口并整合 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.1</version> </dependency> <!-- jsp页面使用的jstl表达式 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--spring核心包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <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.springframework</groupId> <artifactId>spring-context-support</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-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</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-orm</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-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- 数据库 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> <!-- 其他 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> </dependencies> <build> <finalName>resourcemanage</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
导入依赖之后,第一次可能会有import program提示,点击导入即可
接下来我们在resources下新建
三个Spring配置文件:spring-dao.xml,spring-service.xml,spring-web.xml
一个mybatis配置文件:mybatis-config.xml
一个mysql连接的文件:jdbc.properties文件
一个日志配置文件:log.xml
xml文件新建步骤为:右键【resources】-->【New】-->【XML Configuration File】-->【Spring Config】
如果找不到【Spring Config】,说明pom.xml引入依赖没生效,右键一下项目【Maven】-->【Reload project】,然后再重复上一步骤应该就有了。
mysql的配置文件,新建的类型为file就可以
建好之后的结构就是这样的了
接着我们编写XML文件内容
1.jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/sm?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC jdbc.username=root jdbc.password=123456
2.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="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="false"/> <!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 --> <setting name="useGeneratedKeys" value="true"/> <!-- 使用列别名替换列名 默认:true --> <setting name="useColumnLabel" value="true"/> <!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> </configuration>
3.spring-dao.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" xsi:schemaLocation="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"> <!-- 配置整合mybatis过程 --> <!-- 1.配置数据库相关参数properties的属性:${url} --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 2.数据库连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 配置连接池属性 --> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!-- c3p0连接池的私有属性 --> <property name="maxPoolSize" value="30"/> <property name="minPoolSize" value="10"/> <!-- 关闭连接后不自动commit --> <property name="autoCommitOnClose" value="false"/> <!-- 获取连接超时时间 --> <property name="checkoutTimeout" value="10000"/> <!-- 当获取连接失败重试次数 --> <property name="acquireRetryAttempts" value="2"/> </bean> <!-- 3.配置SqlSessionFactory对象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据库连接池 --> <property name="dataSource" ref="dataSource"/> <!-- 配置MyBaties全局配置文件:mybatis-config.xml --> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!-- 扫描entity包 使用别名 --> <property name="typeAliasesPackage" value="com.manage.entity"/> <!-- 扫描sql配置文件:mapper需要的xml文件 --> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> <!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!-- 给出需要扫描Dao接口包 --> <property name="basePackage" value="com.manage.dao"/> </bean> </beans>
这个文件会在扫描sql配置文件处报错,因为映射xml我们还没有放进去,所以我们先给注释掉,防止之后启动项目访问报错。
<!-- 扫描sql配置文件:mapper需要的xml文件 -->
<!--<property name="mapperLocations" value="classpath:mapper/*.xml"/>-->
4.spring-service.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: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/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"> <!-- 扫描service包下所有使用注解的类型 --> <context:component-scan base-package="com.manage.service"/> <!-- 1.配置数据库相关参数properties的属性:${url} --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 2.数据库连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 配置连接池属性 --> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!-- c3p0连接池的私有属性 --> <property name="maxPoolSize" value="30"/> <property name="minPoolSize" value="10"/> <!-- 关闭连接后不自动commit --> <property name="autoCommitOnClose" value="false"/> <!-- 获取连接超时时间 --> <property name="checkoutTimeout" value="10000"/> <!-- 当获取连接失败重试次数 --> <property name="acquireRetryAttempts" value="2"/> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入数据库连接池 --> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置基于注解的声明式事务 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
5.spring-web.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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="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-3.0.xsd"> <!-- 配置SpringMVC --> <!-- 1.开启SpringMVC注解模式 --> <!-- 简化配置: (1)自动注册DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter (2)提供一些列:数据绑定,数字和日期的format @NumberFormat, @DateTimeFormat, xml,json默认读写支持 --> <mvc:annotation-driven/> <!-- 2.静态资源默认servlet配置 (1)加入对静态资源的处理:js,gif,png (2)允许使用"/"做整体映射 --> <mvc:default-servlet-handler/> <!-- 3.配置jsp 显示ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/page/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 4.扫描web相关的bean --> <context:component-scan base-package="com.manage.controller"/> </beans>
6.log.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="true"> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <!-- encoders are by default assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder --> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <root level="debug"> <appender-ref ref="STDOUT"/> </root> </configuration>
以上6个配置文件编写完之后,我们将Spring配置文件挂载到服务启动的时候,在web.xml下配置
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!--1.配置前置控制器 --> <servlet> <servlet-name>SpringDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!-- 引入spring配置文件 --> <param-value>classpath:spring-*.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>SpringDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--2.欢迎文件--> <welcome-file-list> <welcome-file>/page/index.jsp</welcome-file> </welcome-file-list> </web-app>
最后在pom.xml文件中加入对xml的扫描,和pluginManagement同级,也在build标签里面
<!--编译时包括xml文件和properties文件,不然编译后不包括此文件--> <resources> <resource> <directory>src/main/java</directory> <includes> <include>*./*.xml</include> </includes> <filtering>false</filtering> </resource> </resources>
至此,配置文件编写完毕,接下来我们配置一下tomcat看一下效果
四、配置tomcat服务
点击右上角【Add Configuration】-->【+】-->【Tomcat server】-->【Local】
配置sever
切换到【Deployment】,点击【+】,选择第二个带【exploded】的
生成的Application context会带 _war_exploded 后缀,本人习惯删掉,这样保存之后,之前说的【Server】下的url会自动拼上这个值
配置完成之后,点击右上角的绿色三角或者左下角绿色三角,启动tomcat,稍等一会儿浏览器就会自动弹出 当初领你进门的那句话
五、SSM案例
前期的准备工作完成之后, 我们添加一个案例 用户信息维护
1.数据库名sm,表名sysuser,我这里使用的mysql数据库,版本是8.0.26,不知道自己版本的话可以 使用cmd命令查一下版本信息。
mysql -u用户名 -p密码
用户表的DDL语句
CREATE TABLE `sysuser` (
`id` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`username` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`usercode` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`password` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`sex` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`createdate` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`isvalid` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`isstop` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`organid` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
2.生成实体类、xml映射文件。如果没有工具可以参考一下这里 IDEA创建插件自动生成entity、mapper.xml、dao文件
我这里生成好的文件如下
SysUser.java
package com.manage.entity; import java.util.Date; public class SysUser { private Integer id; private String username; private String usercode; private String password; private Integer sex; private Date createdate; private Integer isvalid; private Integer isstop; private Integer organid; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username == null ? null : username.trim(); } public String getUsercode() { return usercode; } public void setUsercode(String usercode) { this.usercode = usercode == null ? null : usercode.trim(); } public String getPassword() { return password; } public void setPassword(String password) { this.password = password == null ? null : password.trim(); } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } public Date getCreatedate() { return createdate; } public void setCreatedate(Date createdate) { this.createdate = createdate; } public Integer getIsvalid() { return isvalid; } public void setIsvalid(Integer isvalid) { this.isvalid = isvalid; } public Integer getIsstop() { return isstop; } public void setIsstop(Integer isstop) { this.isstop = isstop; } public Integer getOrganid() { return organid; } public void setOrganid(Integer organid) { this.organid = organid; } }
SysUser.xml,加上这个文件之后,我们就可以把之前spring-dao.xml中的扫描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.manage.dao.SysUserMapper"> <resultMap id="BaseResultMap" type="com.manage.entity.SysUser"> <id column="id" jdbcType="INTEGER" property="id"/> <result column="username" jdbcType="VARCHAR" property="username"/> <result column="usercode" jdbcType="VARCHAR" property="usercode"/> <result column="password" jdbcType="VARCHAR" property="password"/> <result column="sex" jdbcType="INTEGER" property="sex"/> <result column="createdate" jdbcType="TIMESTAMP" property="createdate"/> <result column="isvalid" jdbcType="INTEGER" property="isvalid"/> <result column="isstop" jdbcType="INTEGER" property="isstop"/> <result column="organid" jdbcType="INTEGER" property="organid"/> </resultMap> <sql id="Base_Column_List"> id, username, usercode, password, sex, createdate, isvalid, isstop, organid </sql> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap"> select <include refid="Base_Column_List"/> from sysuser where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> delete from sysuser where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.manage.entity.SysUser"> insert into sysuser (id, username, usercode, password, sex, createdate, isvalid, isstop, organid) values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{usercode,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{sex,jdbcType=INTEGER}, #{createdate,jdbcType=TIMESTAMP}, #{isvalid,jdbcType=INTEGER}, #{isstop,jdbcType=INTEGER}, #{organid,jdbcType=INTEGER}) </insert> <insert id="insertSelective" parameterType="com.manage.entity.SysUser"> insert into sysuser <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null"> id, </if> <if test="username != null"> username, </if> <if test="usercode != null"> usercode, </if> <if test="password != null"> password, </if> <if test="sex != null"> sex, </if> <if test="createdate != null"> createdate, </if> <if test="isvalid != null"> isvalid, </if> <if test="isstop != null"> isstop, </if> <if test="organid != null"> organid, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null"> #{id,jdbcType=INTEGER}, </if> <if test="username != null"> #{username,jdbcType=VARCHAR}, </if> <if test="usercode != null"> #{usercode,jdbcType=VARCHAR}, </if> <if test="password != null"> #{password,jdbcType=VARCHAR}, </if> <if test="sex != null"> #{sex,jdbcType=INTEGER}, </if> <if test="createdate != null"> #{createdate,jdbcType=TIMESTAMP}, </if> <if test="isvalid != null"> #{isvalid,jdbcType=INTEGER}, </if> <if test="isstop != null"> #{isstop,jdbcType=INTEGER}, </if> <if test="organid != null"> #{organid,jdbcType=INTEGER}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.manage.entity.SysUser"> update sysuser <set> <if test="username != null"> username = #{username,jdbcType=VARCHAR}, </if> <if test="usercode != null"> usercode = #{usercode,jdbcType=VARCHAR}, </if> <if test="password != null"> password = #{password,jdbcType=VARCHAR}, </if> <if test="sex != null"> sex = #{sex,jdbcType=INTEGER}, </if> <if test="createdate != null"> createdate = #{createdate,jdbcType=TIMESTAMP}, </if> <if test="isvalid != null"> isvalid = #{isvalid,jdbcType=INTEGER}, </if> <if test="isstop != null"> isstop = #{isstop,jdbcType=INTEGER}, </if> <if test="organid != null"> organid = #{organid,jdbcType=INTEGER}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.manage.entity.SysUser"> update sysuser set username = #{username,jdbcType=VARCHAR}, usercode = #{usercode,jdbcType=VARCHAR}, password = #{password,jdbcType=VARCHAR}, sex = #{sex,jdbcType=INTEGER}, createdate = #{createdate,jdbcType=TIMESTAMP}, isvalid = #{isvalid,jdbcType=INTEGER}, isstop = #{isstop,jdbcType=INTEGER}, organid = #{organid,jdbcType=INTEGER} where id = #{id,jdbcType=INTEGER} </update> </mapper>
SysUserMapper.java
package com.manage.dao; import com.manage.entity.SysUser; public interface SysUserMapper { int deleteByPrimaryKey(Integer id); int insert(SysUser record); int insertSelective(SysUser record); SysUser selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(SysUser record); int updateByPrimaryKey(SysUser record); }
SysUserService.java
package com.manage.service; import com.manage.entity.SysUser; public interface SysUserService { SysUser getOneById(Integer id); }
SysUserServiceImpl.java
package com.manage.service.impl; import com.manage.dao.SysUserMapper; import com.manage.entity.SysUser; import com.manage.service.SysUserService; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class SysUserServiceImpl implements SysUserService { @Resource public SysUserMapper sysUserMapper; @Override public SysUser getOneById(Integer id) { return sysUserMapper.selectByPrimaryKey(id); } }
SysUserController.java
package com.manage.controller; import com.manage.entity.SysUser; import com.manage.service.SysUserService; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; @Controller @RequestMapping("/user") public class SysUserController { @Resource public SysUserService sysUserService; @RequestMapping("/getInfo") public String getUserInfo(HttpServletRequest request){ SysUser user = sysUserService.getOneById(1); request.setAttribute("user",user); return "userinfo"; } }
再新建一个jsp页面用来展示查出的数据
userinfo.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <div>用户信息</div> <div>用户名:${user.username}</div> <div>用户编码:${user.usercode}</div> </body> </html>
建好之后我们启动程序,然后按照controller里面写的路径访问一下
结果显示页面并没有解析jsp的内置作用域,造成这种原因是因为web.xml是我们新建项目的时候自动生成,需要改一下
将
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app>
修改为
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
我们再重启一下访问发现就可以了。
至此,我们完成了一个SSM框架的搭建和案例的展示。