专栏首页Java探索之路基于SSM框架与Maven的CRUD案例

基于SSM框架与Maven的CRUD案例

项目实现

使用技术

框架:SpringMVC+Spring+Mybatis 数据库:MySQL 项目构建工具:Maven

项目架构设计

传统项目设计模式

mvc设计模式 m:model service层和dao层和实体类层 v:view 视图层jsp页面 c:controller 控制层controller或servlet

Maven项目设计模式

Maven项目设计模式实际上是对mvc设计模式的优化 通过连接远程仓库或镜像仓库或远程仓库,使程序员只需提供相关jar包的坐标就能够自动导入jar包而不需要手动导包,并能够方便对jar包和jdk、tomcat等进行版本控制

Maven的工程类型

POM工程

POM工程是逻辑工程。用在父级工程或聚合工程中。用来做 jar包的版本控制。

JAR工程

将会打包成 jar用作 jar包使用。即常见的本地工程 - Java Project。

WAR工程

将会打包成 war,发布在服务器上的工程。如网站或服务。即常见的网络工程 - Dynamic Web Project。war工程默认没有 WEB-INF目录及 web.xml配置文件,IDE通常会显示工程 错误,提供完整工程结构可以解决。

创建工程

一、创建parent工程

parent工程:用于版本控制。

pom.xml

<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.bjsxt</groupId>
  <artifactId>11parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  
 <properties>
  	    <junit.version>4.12</junit.version>
		<spring.version>4.1.3.RELEASE</spring.version>
		<mybatis.version>3.2.8</mybatis.version>
		<mybatis.spring.version>1.2.2</mybatis.spring.version>
		<mysql.version>5.1.32</mysql.version>
		<slf4j.version>1.6.4</slf4j.version>
		<druid.version>1.0.9</druid.version>
		<jstl.version>1.2</jstl.version>
		<servlet-api.version>2.5</servlet-api.version>
		<jsp-api.version>2.0</jsp-api.version>
		<tomcat.version>2.2</tomcat.version>
  </properties>
  <!-- jar包的依赖注入  ,由于该工程是一个父工程,所以jar包在该pom文件中只是声明-->
  <dependencyManagement>
  	<dependencies>
			<!-- 单元测试 -->
			<dependency>
				<groupId>junit</groupId>
				<artifactId>junit</artifactId>
				<version>${junit.version}</version>
				<scope>test</scope>
			</dependency>
			<!-- 日志处理 -->
			<dependency>
				<groupId>org.slf4j</groupId>
				<artifactId>slf4j-log4j12</artifactId>
				<version>${slf4j.version}</version>
			</dependency>
			<!-- Mybatis -->
			<dependency>
				<groupId>org.mybatis</groupId>
				<artifactId>mybatis</artifactId>
				<version>${mybatis.version}</version>
			</dependency>
			<dependency>
				<groupId>org.mybatis</groupId>
				<artifactId>mybatis-spring</artifactId>
				<version>${mybatis.spring.version}</version>
			</dependency>
			<!-- MySql -->
			<dependency>
				<groupId>mysql</groupId>
				<artifactId>mysql-connector-java</artifactId>
				<version>${mysql.version}</version>
			</dependency>
			<!-- 连接池 -->
			<dependency>
				<groupId>com.alibaba</groupId>
				<artifactId>druid</artifactId>
				<version>${druid.version}</version>
			</dependency>
			<!-- Spring -->
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context</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-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-aspects</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<!-- JSP相关 -->
			<dependency>
				<groupId>jstl</groupId>
				<artifactId>jstl</artifactId>
				<version>${jstl.version}</version>
			</dependency>
			<dependency>
				<groupId>javax.servlet</groupId>
				<artifactId>servlet-api</artifactId>
				<version>${servlet-api.version}</version>
				<scope>provided</scope>
			</dependency>
			<dependency>
				<groupId>javax.servlet</groupId>
				<artifactId>jsp-api</artifactId>
				<version>${jsp-api.version}</version>
				<scope>provided</scope>
			</dependency>
		</dependencies>
  </dependencyManagement>
  
  <!-- 插件的开启 -->
  <build>
  	<!-- tomcat插件,由于子项目不一定每个都是web项目,所以该插件只是声明,并未开启  -->
  	<pluginManagement>
  		<plugins>
				<!-- 配置Tomcat插件 -->
				<plugin>
					<groupId>org.apache.tomcat.maven</groupId>
					<artifactId>tomcat7-maven-plugin</artifactId>
					<version>${tomcat.version}</version>
				</plugin>
			</plugins>
  	</pluginManagement>
  	
  	<!-- maven的编译器插件,该插件主要是决定了当前项目所使用jre版本 。由于无论是jar,还是war项目都需要制定jar的版本,所以该插件不需要生命,应当是开启的。-->
 	<plugins>
 		<plugin>
 		<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>3.2</version>
			 <configuration>
				<source>1.7</source>
				<target>1.7</target>
				<encoding>UTF-8</encoding>
			</configuration>
 		</plugin>
 	</plugins>
  </build>
</project>

二、创建聚合工程Manager

聚合工程:用于将子工程的项目聚合在一起,实现mvc的功能

pom.xml

<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>
	<parent>
		<groupId>com.bjsxt</groupId>
		<artifactId>11parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>12manager</artifactId>
	<packaging>pom</packaging>
	<!-- 这些是后来创建子工程才有的 -->
	<modules>
		<module>manager-pojo</module>
		<module>manager-mapper</module>
		<module>manager-service</module>
		<module>manager-controller</module>
	</modules>
</project>

三、创建子工程manager-pojo

存放的是实体类,相当于mvc的m层

pom.xml

<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>
  <parent>
    <groupId>com.bjsxt</groupId>
    <artifactId>12manager</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>manager-pojo</artifactId>

  <build/>
</project>

四、创建子工程manager-mapper

存放的是mapper层以及mybatis的mapper.xml文件,相当于mvc的m层 创建方法同上,需要手动添加依赖

pomxml

<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>
  <parent>
    <groupId>com.bjsxt</groupId>
    <artifactId>12manager</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>manager-mapper</artifactId>
  
  
   <dependencies>
  	<dependency>
  		 <groupId>com.bjsxt</groupId>
  		 <artifactId>manager-pojo</artifactId>
   		 <version>0.0.1-SNAPSHOT</version>
  	</dependency>
</project>

五、创建子工程manager-service

存放的是service层接口以及实现类,相当于mvc的m层 创建方法同上需要手动添加依赖

pom.xml

<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>
  <parent>
    <groupId>com.bjsxt</groupId>
    <artifactId>12manager</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>manager-service</artifactId>
  
  <dependencies>
  	<dependency>
  		<groupId>com.bjsxt</groupId>
   		 <artifactId>manager-mapper</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  	
  		
  	</dependencies>
  <build/>
</project>

六、创建子工程manager-controller

存放的是控制器,相当于mvc的c层,需要手动添加依赖 注意此处创建的war工程,需要添加web.xml如下图

注:本人配置完毕后pom.xml还报错是因为jdk版本的问题,本人jdk1.8,jdk1.7不会报错,但是不影响程序运行 /jsp/index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
   	<a href="addUser">添加用户</a><br>
   	<a href="/user/findUser">查询用户</a>
  </body>
</html>

pom.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="WebApp_ID" version="3.0">
	<!-- 上下文参数,告诉 Spring 配置文件路径 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/applicationContext-*.xml
		</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	
</web-app> 

框架整合

数据库配置文件(mysql)

CREATE TABLE `users` (
`userid` int(11) NOT NULLAUTO_INCREMENT,
`username` varchar(30) DEFAULT NULL,
`userage` int(11) DEFAULT NULL,
PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
jdbc.driver=com.mysql.jdbc.Driver 
jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8 
jdbc.username=root 
jdbc.password=root

Mybatis配置文件

<?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>
  <!-- 配置分页插件 -->
  </configuration>

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: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.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- 配置解析properties文件的工具类 -->
	<context:property-placeholder location="classpath:*/*.properties"/>
	
	<!-- 配置数据源 dataSource -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
	    <property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="maxActive" value="10" />
		<property name="minIdle" value="5" />
	</bean>
	
	<!-- 创建mybatis的上下文对象  -->
	<bean class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource"/>
		</property>
		<property name="configLocation">
			<value>classpath:mybatis/SqlMapperClient.xml</value>
		</property>
	</bean>
	
	<!-- 扫描mybatis的接口与映射配置文件 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.bjsxt.mapper"/>
	</bean>
</beans>

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: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.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- 扫描bean对象 -->
	<context:component-scan base-package="com.bjsxt.service"/>
</beans>

trans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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/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/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
   <!-- 配置事物管理器的切面 --> 
   <bean id="transactionMananger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>   
   </bean>    
   
   <!-- 配置事物传播行为 :其实就是那些方法应该受什么样的事物控制-->
    <tx:advice id="advice" transaction-manager="transactionMananger">
   	<tx:attributes>
   		<tx:method name="add*" propagation="REQUIRED"/>
   		<tx:method name="modify*" propagation="REQUIRED"/>
   		<tx:method name="update*" propagation="REQUIRED"/>
   		<tx:method name="dorp*" propagation="REQUIRED"/>
   		<tx:method name="del*" propagation="REQUIRED"/>
   		<tx:method name="find*" read-only="true"/>
   	</tx:attributes>
   </tx:advice>
   
   <!-- 那些类下的方法需要参与到当前的事物管理中 。配置切点 -->
   <aop:config>
   	<aop:advisor advice-ref="advice" pointcut="execution(* com.bjsxt.service.*.*(..))"/>
   </aop:config>
</beans>

springmvc.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.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
      <!-- 包的扫描器主要是扫描@controller -->
      <context:component-scan base-package="com.bjsxt.web.controller"/>  

	   <!-- 注册两个新对象 主要是为了来处理springmvc中的其他anntation 如:@requestmapping -->	
	   <mvc:annotation-driven/>
	   
	   <!-- 视图解析器 -->
	   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" /><!-- jsp所在的前缀 -->
		<property name="suffix" value=".jsp" />
    </bean>
    <!-- 配置静态资源映射 -->
    <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>
	<mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>
	<mvc:resources location="/WEB-INF/jsp/" mapping="/jsp/**"/>
</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="WebApp_ID" version="3.0">
	<!-- 上下文参数,告诉 Spring 配置文件路径 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/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>

			<param-name>contextConfigLocation</param-name>
			<param-value>
				classpath:spring/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>
		<filter-name>encoding</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>
	</filter>
	<filter-mapping>
		<filter-name>encoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app> 

配置tomcat插件

<!-- 配置tomcat插件 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<port>8080</port>
					<path>/</path>
				</configuration>
			</plugin>
		</plugins>
	</build>

配置资源拷贝插件

	<!-- 配置资源拷贝插件 -->
  	<build>
  		<resources>
  			<resource>
  				<directory>src/main/java</directory>
				<includes>
					<include>**/*.xml</include>
				</includes>
  			</resource>
  		</resources>
  	</build>

项目打包

右击12manager-》run as -》Maven Install

启动tomcat测试框架整合

右击12manager-》run as -》Maven build…

添加业务逻辑

添加用户

创建实体类user

package com.bjsxt.pojo;

public class User {
	private int userid;
	private String username;
	private int userage;
	public int getUserid() {
		return userid;
	}
	public void setUserid(int userid) {
		this.userid = userid;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public int getUserage() {
		return userage;
	}
	public void setUserage(int userage) {
		this.userage = userage;
	}
	
	
}

创建PageController.java

package com.bjsxt.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.*;

/**
 * 页面跳转controller
 * @author chy
 *
 */
@Controller
public class PageController {
	
	@RequestMapping("/{page}")
	public String showPage(@PathVariable String page) {
		return page;
		
	}
	
}

pom.xml

<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>
	<parent>
		<groupId>com.bjsxt</groupId>
		<artifactId>12manager</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>manager-controller</artifactId>
	<packaging>war</packaging>
	


	<properties>
		<webVersion>4.0</webVersion>
	</properties>
	<dependencies>
		<dependency>
			<groupId>com.bjsxt</groupId>
			<artifactId>manager-service</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>

		<!-- JSP相关 -->
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jsp-api</artifactId>
			<scope>provided</scope>
		</dependency>
	</dependencies>
	<!-- 配置tomcat插件 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<port>8080</port>
					<path>/</path>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
   	<a href="addUser">添加用户</a><br>
   	<a href="/user/findUser">查询用户</a>
  </body>
</html>

ok.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'ok.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <h1>操作成功</h1>
  </body>
</html>

创建 UserService接口 与 UserServiceImpl.java

package com.bjsxt.service;

import java.util.List;

import com.bjsxt.pojo.User;

public interface UserService {
	
	public void addUser(User user);
	
	public List<User> findUser();
}
package com.bjsxt.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bjsxt.mapper.UserMapper;
import com.bjsxt.pojo.User;
@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper usermapper;
	
	@Override
	public void addUser(User user) {
		this.usermapper.insertUser(user);

	}

	@Override
	public List<User> findUser() {

		return this.usermapper.findUser();
	}

}

POM.XML

<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>
  <parent>
    <groupId>com.bjsxt</groupId>
    <artifactId>12manager</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>manager-service</artifactId>
  
  <dependencies>
  	<dependency>
  		<groupId>com.bjsxt</groupId>
   		 <artifactId>manager-mapper</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  	
  		<!-- Spring -->
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-beans</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-webmvc</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-jdbc</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-aspects</artifactId>
			</dependency>
  	
  	</dependencies>
  <build/>
</project>

创建UserMaapper与userMapper.xml

package com.bjsxt.mapper;

import java.util.List;

import com.bjsxt.pojo.User;

public interface UserMapper {
	
	public void insertUser(User user);
	
	public List<User> findUser();
}
<?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.bjsxt.mapper.UserMapper" >

  <insert id="insertUser" parameterType="com.bjsxt.pojo.User">
  		insert into users(username,userage) values(#{username},#{userage})
  </insert>
  
  <select id="findUser" resultType="com.bjsxt.pojo.User">
  		select * from users
  </select> 
</mapper>
package com.bjsxt.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bjsxt.mapper.UserMapper;
import com.bjsxt.pojo.User;
@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper usermapper;
	
	@Override
	public void addUser(User user) {
		this.usermapper.insertUser(user);

	}

	@Override
	public List<User> findUser() {

		return this.usermapper.findUser();
	}

}

pom.xml

<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>
  <parent>
    <groupId>com.bjsxt</groupId>
    <artifactId>12manager</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>manager-service</artifactId>
  
  <dependencies>
  	<dependency>
  		<groupId>com.bjsxt</groupId>
   		 <artifactId>manager-mapper</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  	
  		<!-- Spring -->
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-beans</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-webmvc</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-jdbc</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-aspects</artifactId>
			</dependency>
  	
  	</dependencies>
  <build/>
</project>

打包、测试与运行

查询用户

PageController代码的编写

package com.bjsxt.web.controller;

import java.util.List;

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 com.bjsxt.pojo.User;
import com.bjsxt.service.UserService;

/**
 * 操作UserController
 * @author chy
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	/**
	 * 添加用户的方法
	 */
	@RequestMapping("/addUser")
	public String addUser(User user) {
		this.userService.addUser(user);
		return "ok";
		
	}
	
	/**
	 * 查询用户的方法
	 * /user/findUser
	 */
	@RequestMapping("/findUser")
	public String findUser(Model model) {
		List<User> userlist=this.userService.findUser();
		System.out.println(userlist);
		model.addAttribute("userlist", userlist);
		return "success";
	}
}

jsp页面代码的编写

addUser.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'addUser.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <form action="/user/addUser">
    <!-- 	用户id<input type="text" name="userid"><br> -->
    	姓名<input type="text" name="username"><br>
    	年龄<input type="text" name="userage"><br>
    	<input type="submit" value="确定">
    </form>
  </body>
</html>

success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'success.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    	
    		<table border="1" align="center">
    			<tr>
    				<th>id</th>
    				<th>姓名</th>
    				<th>年龄</th>
    				<th>操作</th>
    			</tr>
    			<c:forEach items="${userlist}" var="user">
    				<tr>
    				<td>${user.userid}</td>
    				<td>${user.username}</td>
    				<td>${user.userage}</td>
    				<td><a href="" >修改</a>&nbsp;&nbsp;<a href="">删除</a></td>
    				</tr>
    			</c:forEach>
    		</table>
    	
  </body>
</html>

UserService接口 与 UserServiceImpl代码的编写

上面代码已写

UserMaapper与userMapper.xml代码的编写

上面代码已写

打包、测试与运行

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 基于Dubbo的CRUD案例

    时间静止不是简史
  • 手把手教你搭建分布式项目环境

    根据电商项目模拟练习后 ,根据个人总结和经验重新进行了分布式项目的框架搭建 , 现总结过程步骤 ,为以后进行相关类型的开发做好基础~~~

    时间静止不是简史
  • Redis项目案例

    项目源码 链接:https://pan.baidu.com/s/1Bat7QS6akuSF4k6MprIFiw 提取码:z23d

    时间静止不是简史
  • 【SpringBoot】SpringBoot基础-HelloWord实例

    SpringBoot的简介我就不写一个篇章来介绍了,相信大家搜索一下,网上一大堆。

    谙忆
  • SSH搭建spring,使用依赖注入的方法

    网上有很多用junit教你搭的,虽然都能跑,但是如果要实际运行和前台交互,那样是不行的,还要导入我所导入的包。

    ydymz
  • 大数据技术之_24_电影推荐系统项目_08_项目总结及补充

    一 数据加载服务1、目标2、步骤二 离线推荐服务2.1 基于统计性算法1、目标2、步骤2.2 基于隐语义模型(LFM)的协同过滤推荐算法(ALS)1、目标2、步...

    黑泽君
  • 本地maven仓库使用及配置

    下载 nexus-2.11.4-01-bundle.zip 解压并运行C:\nexus-2.11.4-01-bundle\nexus-2.11.4-01\bin...

    肖哥哥
  • Neo4j Spatial数据导入

    首先,安装neo4j数据库。我的开发环境是Ubuntu,安装过程参考官网:Neo4j Debian Packages,安装后配置:Post-installati...

    卡尔曼和玻尔兹曼谁曼
  • spring cloud(学习笔记)高可用注册中心(Eureka)的实现(一)

    最近在学习的时候,发现微服务架构中,假如只有一个注册中心,那这个注册中心挂了可怎么办,这样的系统,既不安全,稳定性也不好,网上和书上找了一会,发现这个sprin...

    Dawnzhang
  • 使用 Spring Data 以 Redis 作为数据存储来构建应用 - 第 1 部分

    在本文里面,我将介绍 Java 开发者使用 Spring Data 访问 Redis 并执行操作的编程方式。

    大数据弄潮儿

扫码关注云+社区

领取腾讯云代金券