基于spring+springmvc+hibernate的全注解开发

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u010808777/article/details/81603557

    接触这三者的整合框架也有点时间了,自己在搭建的过程当中也遇到了一些问题,觉得有必要将搭建的这一整个过程写出来希望能够对于现在正在学习这个框架的朋友们有些帮助。这个框架是基于全注解的用maven进行构建管理的,之前也看过类似说是全注解的博客里面与数据库打交道的时候还是得去写对应的(xml)配置文件,觉得并非是真正的基于全注解的开发吧。下面我将详细的步骤写出来供大家参考,后面也会提供源码。

一、新建maven项目(我这里使用开发工具是IDE)

下一步填写相关的信息

下一步,我框红色的地方这里我用的自己下载的maven当然用IDE自带的也是可以的并不用纠结这个,再下一步。

然后完成就可以了,这时候IDE会去下载它所需要的一些文件,等待它自动完成即可。完成之后会看到对应的pom文件中有相关的包被引用进来了,这些都可以删除掉了。我们稍作修改这就是一个干净的pom文件了。

此时对应的文件目录如下:

我们需要差了的文件结构对应补齐。

这时候我们需要在pom中引用我们所需的jar包。pom文件如下:

<?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>cn.lcan</groupId>
  <artifactId>SSH</artifactId>
  <version>1.0</version>
  <packaging>war</packaging>
  <name>${project.artifactId}</name>

  <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>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <hibernate.version>4.3.5.Final</hibernate.version>
    <spring.version>4.2.8.RELEASE</spring.version>
    <activiti>5.19.0</activiti>
  </properties>

  <dependencies>

    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.3.4</version>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpmime</artifactId>
      <version>4.3.4</version>
    </dependency>

    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.0</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-jpa</artifactId>
      <version>1.10.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>5.0.0.Final</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-c3p0</artifactId>
      <version>${hibernate.version}</version>
    </dependency>

    <dependency>
      <groupId>com.oracle</groupId>
      <artifactId>ojdbc6</artifactId>
      <version>11.2.0.3</version>
    </dependency>


    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.34</version>
    </dependency>

    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-spatial</artifactId>
      <version>5.0.0.Final</version>
      <exclusions>
        <exclusion>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-core</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

    <dependency>
      <groupId>javax.mail</groupId>
      <artifactId>mail</artifactId>
      <version>1.4.4</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-web</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-oxm</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.2.2</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-core-asl</artifactId>
      <version>1.9.13</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-mapper-asl</artifactId>
      <version>1.9.13</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-mapper-lgpl</artifactId>
      <version>1.9.13</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-core-lgpl</artifactId>
      <version>1.9.13</version>
    </dependency>

    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.6</version>
    </dependency>
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.2.2</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.4</version>
    </dependency>
    <dependency>
      <groupId>joda-time</groupId>
      <artifactId>joda-time</artifactId>
      <version>2.4</version>
    </dependency>
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>12.0.1</version>
    </dependency>

    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.2.4</version>
    </dependency>

    <dependency>
      <groupId>org.jsoup</groupId>
      <artifactId>jsoup</artifactId>
      <version>1.7.2</version>
    </dependency>

    <dependency>
      <groupId>org.apache.ant</groupId>
      <artifactId>ant</artifactId>
      <version>1.9.3</version>
    </dependency>

    <dependency>
      <groupId>dom4j</groupId>
      <artifactId>dom4j</artifactId>
      <version>1.6.1</version>
    </dependency>
    <dependency>
      <groupId>jaxen</groupId>
      <artifactId>jaxen</artifactId>
      <version>1.1.6</version>
    </dependency>

    <dependency>
      <groupId>org.apache.pdfbox</groupId>
      <artifactId>pdfbox</artifactId>
      <version>1.8.7</version>
    </dependency>


    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>

    <!--json-lib -->
    <dependency>
      <groupId>net.sf.json-lib</groupId>
      <artifactId>json-lib</artifactId>
      <version>2.4</version>
      <classifier>jdk15</classifier>
    </dependency>

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.38</version>
    </dependency>

    <dependency>
      <groupId>org.opensaml</groupId>
      <artifactId>opensaml1</artifactId>
      <version>1.1</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!--    <dependency>
         <groupId>org.geotools</groupId>
         <artifactId>gt-shapefile</artifactId>
         <version>13.2</version>
       </dependency> -->

    <dependency>
      <groupId>com.github.junrar</groupId>
      <artifactId>junrar</artifactId>
      <version>0.7</version>
    </dependency>

    <dependency>
      <groupId>net.lingala.zip4j</groupId>
      <artifactId>zip4j</artifactId>
      <version>1.3.2</version>
    </dependency>

    <!-- <dependency> <groupId>com.github.axet</groupId> <artifactId>kaptcha</artifactId>
        <version>0.0.9</version> </dependency> -->

    <dependency>
      <groupId>org.json</groupId>
      <artifactId>json</artifactId>
      <version>20140107</version>
    </dependency>

    <dependency>
      <groupId>net.sourceforge.jexcelapi</groupId>
      <artifactId>jxl</artifactId>
      <version>2.6.12</version>
    </dependency>

    <dependency>
      <groupId>com.github.pfmiles</groupId>
      <artifactId>min-velocity</artifactId>
      <version>1.1.0</version>
    </dependency>


    <!--redis -->
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>

    <dependency>
      <groupId>com.belerweb</groupId>
      <artifactId>pinyin4j</artifactId>
      <version>2.5.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.velocity</groupId>
      <artifactId>velocity</artifactId>
      <version>1.7</version>
    </dependency>

    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.9</version>
    </dependency>

    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>3.9</version>
    </dependency>

    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml-schemas</artifactId>
      <version>3.9</version>
    </dependency>

    <dependency>
      <groupId>org.apache.xmlbeans</groupId>
      <artifactId>xmlbeans</artifactId>
      <version>2.3.0</version>
    </dependency>

    <dependency>
      <groupId>org.xerial.snappy</groupId>
      <artifactId>snappy-java</artifactId>
      <version>1.1.2.6</version>
    </dependency>
    <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>
  </dependencies>

  <build>   <!--加入Tomcat插件方便发布到Tomcat中以及运行和调试-->
    <finalName>ssh</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.tomcat.maven</groupId>
          <artifactId>tomcat7-maven-plugin</artifactId>
          <version>2.1</version>
          <configuration>
            <port>8091</port>
            <uriEncoding>utf-8</uriEncoding>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>


</project>

这时候我们新建自己的目录结构:

在resources中新建一个jdbc.properties文件。

#database connection config
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost/ssh?useUnicode=true&characterEncoding=utf-8
jdbc.username = root
jdbc.password = 

#hibernate config
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=false
hibernate.connection.characterEncoding=UTF-8


c3p0.minPoolSize=1
c3p0.maxPoolSize=1000
c3p0.acquireIncrement=1
c3p0.maxIdleTime=30
c3p0.idleConnectionTestPeriod=20
c3p0.testConnectionOnCheckin=true
c3p0.preferredTestQuery=select 1
c3p0.acquireRetryAttempts=3

新建对应的数据库,然后新建一张表person

 

这时候我们在entity新建一个Person类,编写对应的实体类:

package cn.lcan.entity;


import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.util.Date;


@Entity
@Table(name = "person")
@XmlRootElement(name = "Person")
public class Person implements Serializable{
    @Id
    @Column(name = "id")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")//定于主键的生成策略
	@GeneratedValue(generator = "system-uuid")
    private String id;

    @Column(name = "created")
    private Date created = new Date();

    @Column(name = "username")
    private String username;

    @Column(name = "address")
    private String address;

    @Column(name = "phone")
    private String phone;

    @Column(name = "remark")
    private String remark;

	public String getId() {
		return id;
	}

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

	public Date getCreated() {
		return created;
	}

	public void setCreated(Date created) {
		this.created = created;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}
    
    
}

编写PersonDao:

package cn.lcan.dao;

import cn.lcan.entity.Person;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;


@Repository
public interface PersonDao extends CrudRepository<Person, String>, JpaSpecificationExecutor<Person>{

	

}

编写service以对应的实现类

package cn.lcan.service;

public interface IpersionServer {



}
package cn.lcan.service.impl;

import cn.lcan.dao.PersonDao;
import cn.lcan.entity.Person;
import cn.lcan.service.IpersionServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class PersionImpl implements IpersionServer {
	
	@Autowired
	private PersonDao personDao;

	
	

}

然后编写action(里面写一个test是为了待会儿测试springmvc框架是否成功的):

package cn.lcan.action;

import cn.lcan.service.IpersionServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class PersionAction {
	
	@Autowired
    IpersionServer persionServer;
	
	@RequestMapping(value="/test",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String Test(){
		System.out.println("asfsadfsadfsda");
		return "success";
	}
	
}

这时候我们需要编写相关的配置文件了,新建spring的配置文件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" xmlns:util="http://www.springframework.org/schema/util"
	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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
	default-autowire="byName">

	<!-- 开始spring mvc的注解 ignoreDefaultModelOnRedirect="true" -->
	<mvc:annotation-driven />

	<!-- start开启注释 --><!-- 配置注解扫描的包的路径 -->
	<context:component-scan base-package="cn.lcan">
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Service" />
	</context:component-scan>
	<!-- end开始注释 -->

</beans>

然后新建portal-context.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:jdbc="http://www.springframework.org/schema/jdbc"
	   xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
	   xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	   xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:cache="http://www.springframework.org/schema/cache"
	   xmlns:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p"
	   xsi:schemaLocation="
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd  
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd   
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd   
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd ">

	<aop:aspectj-autoproxy />
	<context:annotation-config />

	<context:component-scan base-package="cn.lcan" />

	<context:property-placeholder location="classpath:jdbc.properties"
		ignore-unresolvable="true" order="200" />


	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="${jdbc.driver}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="minPoolSize" value="${c3p0.minPoolSize}" />
		<property name="maxPoolSize" value="${c3p0.maxPoolSize}" />
		<property name="acquireIncrement" value="${c3p0.acquireIncrement}" />
		<property name="maxIdleTime" value="${c3p0.maxIdleTime}" />
		<property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}" />
		<property name="testConnectionOnCheckin" value="${c3p0.testConnectionOnCheckin}" />
		<property name="preferredTestQuery" value="${c3p0.preferredTestQuery}" />
		<property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}" />
	</bean>

	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
	<!-- Entity Manager -->
	<bean id="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="packagesToScan" value="cn.lcan.entity" />
		<property name="jpaVendorAdapter">
			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
		</property>
		<property name="jpaProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<!-- <prop key="hibernate.spatial.connection_finder">${hibernate.spatial.connection_finder}</prop> -->
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
				<prop key="hibernate.spatial.ogc_strict">${hibernate.spatial.ogc_strict}</prop>
			</props>
		</property>
	</bean>

	<!-- Transaction Manager -->
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="dataSource" ref="dataSource" />
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>
	<!-- 使用annotation定义事务 -->
	<tx:annotation-driven transaction-manager="transactionManager"
		proxy-target-class="true" />

	<!--配置jpa repository,核心部分 -->
	<jpa:repositories base-package="cn.lcan.dao"
					  entity-manager-factory-ref="entityManagerFactory"
					  transaction-manager-ref="transactionManager" />


	<bean id="springSessionJdbcOperations" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>

</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"
         version="3.0">

    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>

    <!--加载Spring的配置文件到上下文中去-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:/portal-context.xml</param-value>
    </context-param>

    <!-- spring MVC config start-->
    <servlet>
        <servlet-name>spring</servlet-name>
        <!--DispatcherServlet这是spring mvc的核心-->
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:/springmvc.xml</param-value>
        </init-param>
        <!--配置容器在启动的时候就加载这个servlet并实例化-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>  <!--拦截所有请求,拦截后web容器会将拦截的请求交给DispatcherServlet处理-->
    </servlet-mapping>
    <!-- spring MVC config end-->

    <!-- Spring监听器 -->
    <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>
        <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>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

这时候我们新建编译和运行的命令:

 

然后先将项目打包,然后运行,运行后看到控制台输入这个说明成功了:

然后输入http://localhost:8091/SSH到浏览器看能够有这个输出说明成功了(这个Hello World!是index.jsp中内容,端口为啥是8091因为在pom中配置了的。):

这时我们访问http://localhost:8091/SSH/test应该也是能够成功的。

现在在action中我们增加保存和查询的方法修改后如下:

package cn.lcan.action;

import cn.lcan.entity.Person;
import cn.lcan.service.IpersionServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.UUID;

@Controller
public class PersionAction {
	
	@Autowired
    IpersionServer persionServer;
	
	@RequestMapping(value="/test",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String Test(){
		System.out.println("asfsadfsadfsda");
		return "success";
	}

	@RequestMapping(value="/save",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String savePersion(){
		Person person = new Person();
		person.setUsername("xiaoming");
		person.setAddress("hubei");
		person.setCreated(new Date());
		person.setPhone("1111111");
		person.setRemark("3333");
		persionServer.savePerson(person);
		return "保存成功";
	}
	
	@RequestMapping(value="/search",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String PersonName(){
		Person person = persionServer.findById("1");
		System.out.println(person.getUsername());
		return person.getUsername();
	}
	
	
	

}

修改对应的service和serviceImpl:

package cn.lcan.service;

import cn.lcan.entity.Person;

public interface IpersionServer {

    Person findById(String string);

    void savePerson(Person person);
}

 

package cn.lcan.service.impl;

import cn.lcan.dao.PersonDao;
import cn.lcan.entity.Person;
import cn.lcan.service.IpersionServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;


@Service
public class PersionImpl implements IpersionServer {
	
	@Autowired
	private PersonDao personDao;

	@Override
	public Person findById(String id) {
		Person person = personDao.findPersonById(id);
		return person;
	}

	@Override
	@Transactional
	public void savePerson(Person person) {
		personDao.save(person);
	}


}

 

这时候通过浏览器访问http://localhost:8091/SSH/save就能够往数据库中插入数据了,查询的访问对应的查询url即可(我写的那个 persionServer.findById("1");这个1只是为了起一个例子的作用哈)。到这里框架就基本上搭建完成了。这个例子很适合刚刚接触这个框架入门者,由于本人水平有限如果有不正确的地方欢迎批评指正。下载地址为:https://download.csdn.net/download/u010808777/10599138

展开阅读全文

没有更多推荐了,返回首页