[置顶] 抛弃ssh,拥抱ssm

听说企业中ssm运用的很多,而且,近期感觉项目中运用的表关联相当多,运用mybatis感觉会更加方便和灵活,所以准备开始ssm的学习。

初步计划

后台:spring4.1.6+spring mvc4.1.6+mybatis3.2.8+mybatis-spring1.2.2

前台:dojo1.10.4

通过maven来管理jar包

主要采用rest风格。

应广大网友的要求,这里贴出源码,方便网友测试:我是源代码

补充一下:源代码里的pom.xml里面缺少了一个jar包,需要补充一下

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


201507181134最新修正:修改json相关信息,解决json报406的错误

pom.xml里面添加以下jar包即可

      

 <jackson.version>2.2.3</jackson.version>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-jaxb-annotations</artifactId>
            <version>${jackson.version}</version>
        </dependency>

20150725修正:重大的bug,不过找到了总比没找打强。

在spring.xml中缺少一项配置,导致mybatis.xml文件没有起作用,具体是缺少configLocation属性。今天把这个错误修正,这个框架一定会越来越完善,越来越好!

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="configLocation" value="classpath:mybatis.xml"/>
    <property name="dataSource" ref="myC3P0DataSource"></property>
    <property name="mapperLocations" value="classpath:com/aodong/mybatisTest/mapper/*.xml"/>  
    <property name="typeAliasesPackage" value="com.aodong.mybatisTest.model"></property>
</bean>


----------------------------分割线,我们开始吧-----------------------------------


首先,项目截图:

[置顶] 抛弃ssh,拥抱ssm_第1张图片


sql文件:

-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `age` int(11) DEFAULT NULL,
  `name` varchar(32) DEFAULT NULL,
  `niceName` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', '1', '11', '111');
INSERT INTO `student` VALUES ('13', '111', 'haha', 'haha');

web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>mavenChapter1</display-name>
  <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>
  
<!-- 创建spring监听器 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- spring的监听器可以通过这个上下文参数来获取applicationContext.xml的位置 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>
  		classpath:spring.xml
  	</param-value>
  </context-param>
  
  <servlet>
  	<servlet-name>spring-mvc</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>
  </servlet>
  <servlet-mapping>
  	<servlet-name>spring-mvc</servlet-name>
	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <filter>
  	<filter-name>CharacterFilter</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>CharacterFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>

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>mavenChapter2</groupId>
  <artifactId>mavenChapter2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>mavenChapter2</name>
  <description/>
  <properties>
        <!-- spring版本号 -->
        <spring.version>4.1.6.RELEASE</spring.version>
        <!-- mybatis版本号 -->
        <mybatis.version>3.2.8</mybatis.version>
        <!-- log4j日志文件管理包版本 -->
        <slf4j.version>1.7.7</slf4j.version>
        <log4j.version>1.2.17</log4j.version>
        <jackson.version>2.2.3</jackson.version>
    </properties>

    <dependencies>
    <dependency>  
        <groupId>org.aspectj</groupId>  
        <artifactId>aspectjweaver</artifactId>  
        <version>1.6.10</version>  
    </dependency> 
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>7.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.glassfish.web</groupId>
      <artifactId>javax.servlet.jsp.jstl</artifactId>
      <version>1.2.2</version>
    </dependency>
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <!-- 表示开发的时候引入,发布的时候不会加载此包 -->
            <scope>test</scope>
        </dependency>
        <!-- spring核心包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.6.RELEASE</version>
        </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-jdbc</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-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>
        <!-- 导入Mysql数据库链接jar包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.30</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>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-jaxb-annotations</artifactId>
            <version>${jackson.version}</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>
    
</project>

jdbc.properties文件:

datasource.type=mysql
datasource.driverClassName=com.mysql.jdbc.Driver
datasource.url=jdbc\:mysql\://127.0.0.1\:3306/shtest?useUnicode\=true&characterEncoding\=UTF-8
datasource.username=root
datasource.password=root
hibernate.dialect=org.hibernate.dialect.MySQLDialect

#datasource.type=oracle
#datasource.driverClassName=oracle.jdbc.driver.OracleDriver
#datasource.url=jdbc\:oracle\:thin\:@192.168.1.88\:1521\:juice?useUnicode\=true&characterEncoding\=utf-8 
#datasource.username=root
#datasource.password=root
#hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

datasource.maxActive=100
datasource.initialSize=5
datasource.maxIdle=20
datasource.maxWait=120000
datasource.whenExhaustedAction=1
#datasource.validationQuery=select 1 from dual
datasource.testOnBorrow=true
datasource.testOnReturn=false
c3p0.acquireIncrement=3
c3p0.initialPoolSize=3
c3p0.idleConnectionTestPeriod=60
c3p0.minPoolSize=2
c3p0.maxPoolSize=50
c3p0.maxStatements=100
c3p0.numHelperThreads=10
c3p0.maxIdleTime=600
c3p0.testConnectionOnCheckout=false
c3p0.preferredTestQuery=select 1 from dual

log4j.properties文件:

log4j.rootCategory=INFO, stdout , R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[springTest] %p [%t] %C.%M(%L) | %m%n

log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=d\:logsshTest.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n

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="cacheEnabled" value="true" /> -->
		<!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载 -->
		<!-- <setting name="lazyLoadingEnabled" value="true" /> -->
		<!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载 -->
		<!-- <setting name="aggressiveLazyLoading" value="true" /> -->
		<!-- 允许或不允许多种结果集从一个单独的语句中返回(需要适合的驱动) -->
		<!-- <setting name="multipleResultSetsEnabled" value="true" /> -->
		<!-- 使用列标签代替列名。不同的驱动在这方便表现不同。参考驱动文档或充分测试两种方法来决定所使用的驱动 -->
		<!-- <setting name="useColumnLabel" value="true" /> -->
		<!-- 允许JDBC支持生成的键。需要适合的驱动。如果设置为true则这个设置强制生成的键被使用,尽管一些驱动拒绝兼容但仍然有效(比如Derby) -->
		<!-- <setting name="useGeneratedKeys" value="true" /> -->
		<!-- 指定MyBatis如何自动映射列到字段/属性。PARTIAL只会自动映射简单,没有嵌套的结果。FULL会自动映射任意复杂的结果(嵌套的或其他情况) -->
		<!-- <setting name="autoMappingBehavior" value="PARTIAL" /> -->
		<!-- 配置默认的执行器。SIMPLE执行器没有什么特别之处。REUSE执行器重用预处理语句。BATCH执行器重用语句和批量更新 -->
		<!-- <setting name="defaultExecutorType" value="SIMPLE" /> -->
		<!-- 设置超时时间,它决定驱动等待一个数据库响应的时间 -->
		<!-- <setting name="defaultStatementTimeout" value="25000" /> -->
	<!-- </settings> -->

	<!-- 设置别名 -->
	<!-- <typeAliases>
		<typeAlias alias="User" type="com.adon.model.User" />
	</typeAliases> -->
	
</configuration>

spring-mvc.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-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/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd" >

	<!-- Spring为我们提供了一种极为方便注册这些BeanPostProcessor的方式,
		即使用<context:annotation- config/>隐式地向 Spring容器注册
		AutowiredAnnotationBeanPostProcessor、
		RequiredAnnotationBeanPostProcessor、
		CommonAnnotationBeanPostProcessor以及
		PersistenceAnnotationBeanPostProcessor这4个BeanPostProcessor -->
    <context:annotation-config />
    
    <!-- <mvc:annotation-driven /> 是一种简写形式,
    	会自动注册DefaultAnnotationHandlerMapping与
    	AnnotationMethodHandlerAdapter 两个bean,
    	是spring MVC为@Controllers分发请求所必须的 -->
	<mvc:annotation-driven />
	
	<!--使Spring支持自动检测组件,如注解的Controller -->
	<context:component-scan base-package="com.aodong.mybatisTest.controller" />
	
	<!-- 将静态文件指定到某个特殊的文件夹中统一处理 -->
	<mvc:resources location="/resources/" mapping="/resources/**"/>

	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
	
</beans>

spring.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:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xsi:schemaLocation="
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/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/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd">

	<!-- 启用spring注解支持 -->
	<context:annotation-config />
	<context:component-scan base-package="com.aodong.mybatisTest" />
	
	<!-- 加载jdbc.properties文件,用于下边的数据库配置 -->  
    <context:property-placeholder location="classpath:jdbc.properties"/> 
	
	<!-- 配置c3p0连接池 开始-->
	<bean id="myC3P0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${datasource.driverClassName}"></property>  
	    <property name="jdbcUrl" value="${datasource.url}"></property>  
	    <property name="user" value="${datasource.username}"></property>  
	    <property name="password" value="${datasource.password}"></property>  
	    <property name="acquireIncrement" value="${c3p0.acquireIncrement}"></property>  
	    <property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property>  
	    <property name="maxIdleTime" value="${c3p0.maxIdleTime}"></property>  
	    <property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property>  
	    <property name="minPoolSize" value="${c3p0.minPoolSize}"></property>  
	    <property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"></property>  
	    <property name="maxStatements" value="${c3p0.maxStatements}"></property>  
	    <property name="numHelperThreads" value="${c3p0.numHelperThreads}"></property>  
	    <property name="testConnectionOnCheckout" value="${c3p0.testConnectionOnCheckout}"></property>  
	    <property name="preferredTestQuery" value="${c3p0.preferredTestQuery}"></property>  
	    <property name="acquireRetryDelay" value="1000"></property>  
	    <property name="acquireRetryAttempts" value="60"></property>  
	    <property name="breakAfterAcquireFailure" value="false"></property>  
	</bean>
	<!-- 配置c3p0连接池 结束-->
	
	<!--创建sqlSessionFactory 开始 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="myC3P0DataSource"></property>
		<property name="mapperLocations" value="classpath:com/aodong/mybatisTest/mapper/*.xml"/>  
		<property name="typeAliasesPackage" value="com.aodong.mybatisTest.model"></property>
	</bean>
	<!--创建sqlSessionFactory 结束 -->
	
	<!-- 扫描sql映射文件  开始-->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.aodong.mybatisTest.dao"></property>
	</bean>
	<!-- 扫描sql映射文件  结束-->
	
	<!-- 配置事务管理器  开始 -->
	<bean id="txmanager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="myC3P0DataSource"></property>
	</bean>
	<!-- 配置事务管理器   结束 -->
	
	<!--基本事务定义,使用transactionManager作事务管理,默认get*,find*方法的事务为read-only-->
	<tx:advice id="txAdvice" transaction-manager="txmanager">
		<tx:attributes>
			<tx:method name="find*" read-only="true" />
			<tx:method name="get*" read-only="true" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>	   
    <!-- 以AspectJ方式 定义AOP
	         第一个*代表所有的返回值类型 
	         第二个*代表所有的类
	         第三个*代表类所有方法 
	         最后一个..代表所有的参数。
     -->
	<aop:config>
        <aop:pointcut id="interceptorPointCuts" expression="execution(* com.aodong.mybatisTest.service.*Impl.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
	</aop:config>

</beans>

student.java文件:

package com.aodong.mybatisTest.model;

import java.io.Serializable;

public class Student implements Serializable {
	private long id;
	private int age;
	private String name;
	private String niceName;
	
	public Student() {
		
	}
	
	public Student(long id, int age, String name, String niceName) {
		super();
		this.id = id;
		this.age = age;
		this.name = name;
		this.niceName = niceName;
	}

	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getNiceName() {
		return niceName;
	}
	public void setNiceName(String niceName) {
		this.niceName = niceName;
	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", age=" + age + ", name=" + name
				+ ", niceName=" + niceName + "]";
	}
	
}

StudentMapper.java文件:

package com.aodong.mybatisTest.dao;

import java.util.List;

import com.aodong.mybatisTest.model.Student;

public interface StudentMapper {
	public int add(Student stu);
	public int update(Student stu);
	public int delete(long id);
	public Student getStu(long id);
	public List<Student> getStuAll();
}

StudentMapper.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.aodong.mybatisTest.dao.StudentMapper">
	<select id="getStu" parameterType="long" resultType="Student">
        select * from student where id=#{id}
    </select>
    <insert id="add" parameterType="Student">
        insert into Student (age, name, niceName) values (#{age}, #{name}, #{niceName})
    </insert>
    <update id="update" parameterType="Student">
        update Student set age=#{age}, name=#{name}, niceName=#{niceName} where id=#{id}
    </update>
    <delete id="delete" parameterType="long">
        delete from Student where id=#{id}
    </delete>
    <select id="getStuAll" resultType="Student">
        select * from Student;
    </select>
    
</mapper>

StudentService.java文件:

package com.aodong.mybatisTest.service;

import com.aodong.mybatisTest.model.Student;

public interface StudentService {
    public Student getStu(long id);
    public void saveStu(Student stu);
    public void updateStu(Student stu);
    public void deleteStu(long id);
    public List<Student> getStuAll();
}

StudentServiceImpl.java文件

package com.aodong.mybatisTest.service.impl;

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

import com.aodong.mybatisTest.dao.StudentMapper;
import com.aodong.mybatisTest.model.Student;
import com.aodong.mybatisTest.service.StudentService;

@Service("studentService")
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    public Student getStu(long id) {
        return studentMapper.getStu(id);
    }

    public void saveStu(Student stu) {
        studentMapper.add(stu);
    }

    public void updateStu(Student stu) {
        studentMapper.update(stu);
    }

    public void deleteStu(long id) {
        studentMapper.delete(id);
    }

    public List<Student> getStuAll() {
        return studentMapper.getStuAll();
    }
	
}

StudentController.java文件:

package com.aodong.mybatisTest.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.aodong.mybatisTest.model.Student;
import com.aodong.mybatisTest.service.StudentService;

@Controller
@RequestMapping("/student")
public class StudentController {
	@Autowired
	private StudentService studentService;
	
	@RequestMapping(value="/{userid}/show", method=RequestMethod.GET)
	public String show(@PathVariable long userid, Model model){
		Student stu = studentService.getStu(userid);
		System.out.println(stu);
		model.addAttribute("student", studentService.getStu(userid));
		return "student/show";
	}
}

-----------------------------到此基本可以告一段落了--------------------------------------


你可能感兴趣的:([置顶] 抛弃ssh,拥抱ssm)