這是我一年前做的專案的,算是SSM框架入門的例項,maven管理工具所用到的倉庫私服為阿里雲的倉庫。
這裡寫圖片描述

整個專案的目錄:
這裡寫圖片描述

jdbc.properties是對資料庫資訊的配置:

#mysql version database setting
jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=
jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/test?useUnicode=true&characterEncoding=UTF-8

service-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:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
    http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.0.xsd"
	default-autowire="byName" default-lazy-init="true">

	<description>Spring-database配置</description>
	
	<context:component-scan base-package="com.HelloWorld">
		<context:exclude-filter type="annotation" 
				expression="org.springframework.stereotype.Controller" />
	</context:component-scan>
	
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath*:jdbc.properties</value>
			</list>
		</property>
	</bean>

	<!-- mybatis Dao -->
	<bean id="myBatisDao" class="com.HelloWorld.orm.mybatis.MyBatisDao" />

	<!-- 資料來源配置,使用應用內的DBCP資料庫連線池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<!-- Connection Info -->
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />

		<!-- Connection Pooling Info -->
		<property name="initialSize" value="5" />
		<property name="maxActive" value="100" />
		<property name="maxIdle" value="30" />
		<property name="maxWait" value="500" />
		<property name="poolPreparedStatements" value="false" />
		<property name="defaultAutoCommit" value="false" />
	</bean>

	<!-- mybatis配置 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:mybatis-config.xml" />
	</bean>
	
	<!-- 事務管理器配置,單資料來源事務 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<aop:config proxy-target-class="true">
		<aop:advisor pointcut="execution(* com.bolo.examples..*Manager.*(..))" advice-ref="txAdvice" />
	</aop:config>

	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true" />
			<tx:method name="find*" read-only="true" />
			<tx:method name="query*" read-only="true" />
			<tx:method name="is*" read-only="true" />
			<tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />
		</tx:attributes>
	</tx:advice>
</beans>

servlet-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:context="http://www.springframework.org/schema/context" 
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.0.xsd 
	http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

	<description>Spring-web MVC配置</description>
	
	<!-- 對所有類進行掃描,以完成Bean建立和自動依賴注入的功能 -->
	<context:component-scan base-package="com.HelloWorld">
		<context:exclude-filter type="annotation" 
				expression="org.springframework.stereotype.Service" />
	</context:component-scan>
	
	<!-- 當請求應用程式首頁時,將執行/hello請求,還可以設定成redirect -->
	<mvc:view-controller path="/" view-name="forward:/hello" />

	<!-- 這兩個類用來啟動基於Spring MVC的註解功能,將控制器與方法對映加入到容器中 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
	
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
		<property name="cache" value="true" />
		<property name="prefix" value="" />
		<!-- 模板字尾,指定html頁面為模板 -->
		<property name="suffix" value=".html" />
		<!-- 使用這個模板類來解析檢視 -->
		<property name="viewClass"
			value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
		<property name="exposeSpringMacroHelpers" value="true" />
		<!-- 允許訪問請求屬性,預設為false -->
		<property name="exposeRequestAttributes" value="true" />
		<!-- 允許訪問會話屬性,預設為false -->
		<property name="exposeSessionAttributes" value="true" />
		<!-- 頁面上下文,類似於request.contextPath -->
		<property name="requestContextAttribute" value="rc" />
		<!-- 模板輸出內容編碼,此處應與defaultEncoding保持一致 -->
		<property name="contentType" value="text/html;charset=UTF-8" />
	</bean>

	<bean id="freemarkerConfig"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<!-- 模板檔案所在目錄 -->
		<property name="templateLoaderPath" value="/pages/" />
		<!-- 指定FreeMarker模板檔案的編碼格式 -->
		<property name="defaultEncoding" value="UTF-8" />
		<!-- FreeMarker屬性配置 -->
		<property name="freemarkerSettings">
			<props>
				<!-- 
					每隔5s檢查模板是否更新,單位為秒
					如果不經常更新模板可將更新的延遲時間設定長一點
				-->
				<prop key="template_update_delay">5</prop>

				<!-- 指定地區語言環境,我們的語言是中文 -->
				<prop key="locale">zh_CN</prop>
			
			</props>
		</property>
	</bean>
	
</beans>

mybatis-config.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>
		<!-- changes from the defaults -->
		<setting name="lazyLoadingEnabled" value="false" />
	</settings>
    
    <typeAliases>
		<typeAlias alias="Stu" type="com.HelloWorld.examples.base.entity.Student" />
	</typeAliases>
    <mappers>
    	<mapper resource="mapper/StudentMapper.xml"/>
    </mappers>
</configuration>

StudentMapper.xml,SQL語句編寫的地方:

<?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="StudentMapper">

	<sql id="base_column">
	    id,name,chinese,math,english
	</sql>
	
	<select id="selectByEntity" parameterType="Stu" resultType="Stu">
		select 
		<include refid="base_column" />
		from student
	</select>
	
	<select id="selectByPrimaryKey" parameterType="int" resultType="Stu">
		select 
		<include refid="base_column" />
		from student where id = #{id}
	</select>

	
	<insert id="insert" parameterType="Stu">
		INSERT INTO student(name,chinese,math,english) VALUES (#{name}, #{chinese},#{math},#{english});
	</insert>
	
	<update id="update" parameterType="Stu">
		update student
		<set>
			<if test="name != null">
				name = #{name},
				chinese =#{chinese},
				math =#{math},
				english=#{english}
			</if>
		</set>
		where id = #{id}
	</update>

	<delete id="delete" parameterType="int">
		delete from student where id = #{id}
	</delete>
</mapper>

MyBatis的Dao基類:

package com.HelloWorld.orm.mybatis;

import java.io.Serializable;
import java.util.List;

import org.mybatis.spring.support.SqlSessionDaoSupport;

/**
 * MyBatis的Dao基類
 * @author 
 */
public class MyBatisDao extends SqlSessionDaoSupport{
	
	public void save(String key, Object object) {
		getSqlSession().insert(key, object);
	}
	
	public void delete(String key, Serializable id) {
		getSqlSession().delete(key, id);
	}

	public void delete(String key, Object object) {
		getSqlSession().delete(key, object);
	}
	

	public <T> T get(String key, Object params) {
		return (T) getSqlSession().selectOne(key, params);
	}
	

	public <T> List<T> getList(String key) {
		return getSqlSession().selectList(key);
	}

	public <T> List<T> getList(String key, Object params) {
		return getSqlSession().selectList(key, params);
	}
}

entity實體類:

package com.HelloWorld.examples.base.entity;

public class Student {
	private Integer id;
	private String name;
	private float chinese;
	private float  math;
	private float english;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public float getChinese() {
		return chinese;
	}
	public void setChinese(float chinese) {
		this.chinese = chinese;
	}
	public float getMath() {
		return math;
	}
	public void setMath(float math) {
		this.math = math;
	}
	public float getEnglish() {
		return english;
	}
	public void setEnglish(float english) {
		this.english = english;
	}


	
	
}

Service層

package com.HelloWorld.examples.base.service;

import java.io.Serializable;
import java.util.List;

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


import com.HelloWorld.examples.base.entity.Student;
import com.HelloWorld.orm.mybatis.MyBatisDao;


@Service
public class StudentService {
	@Autowired
	private MyBatisDao myBatisDao;
	
	public List<Student> getStudent(){
		return myBatisDao.getList("StudentMapper.selectByEntity");
	}
	
	public Student getStudent(Serializable id){
		return myBatisDao.get("StudentMapper.selectByPrimaryKey",id);
	}
	
	public void save(Student student){
		   
		if(student.getId()==null)
			myBatisDao.save("StudentMapper.insert", student);
		else
			myBatisDao.save("StudentMapper.update",student);
	}

	
	public void delete(Serializable id){
		myBatisDao.delete("StudentMapper.delete", id);
	}
}

controller層:

package com.HelloWorld.examples.base.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.HelloWorld.examples.base.entity.Student;
import com.HelloWorld.examples.base.service.StudentService;

@Controller
@RequestMapping("/student")
public class StudentController {
	@Autowired
	private StudentService studentService;
	
	@RequestMapping(method = RequestMethod.GET)
	public String list(Model model){
		model.addAttribute("list", studentService.getStudent());
		return "base/student_list";
	}
	
	@RequestMapping(value="/new", method = RequestMethod.GET)
	public String create(Model model, Student student){
		model.addAttribute("entity",student);
		return "base/student_new";
	}
	
	@RequestMapping(value="/edit/{id}", method = RequestMethod.GET)
	public String edit(Model model, @PathVariable Integer id){
		model.addAttribute("entity", studentService.getStudent(id));
		return "base/student_edit";
	}
	
	@RequestMapping(value="/save", method = RequestMethod.POST)
	public String save(Model model, Student student){
		studentService.save(student);
		return "redirect:/student";
	}
	
	@RequestMapping(value="/view/{id}", method = RequestMethod.GET)
	public String view(Model model, @PathVariable Integer id){
		model.addAttribute("entity", studentService.getStudent(id));
		return "base/student_view";
	}
	
	@RequestMapping(value="/delete/{id}", method = RequestMethod.GET)
	public String delete(Model model, @PathVariable Integer id){
		studentService.delete(id);
		return "redirect:/student";
	}
}

執行結果:

這裡寫圖片描述

這裡寫圖片描述