基于SSM框架学生管理系统的实现
2017-08-25 09:33
1131 查看
这是我一年前做的项目的,算是SSM框架入门的实例,maven管理工具所用到的仓库私服为阿里云的仓库。
整个项目的目录:
jdbc.properties是对数据库信息的配置:
service-context.xml:
servlet-context.xml:
mybatis-config.xml:
StudentMapper.xml,SQL语句编写的地方:
MyBatis的Dao基类:
entity实体类:
Service层
controller层:
执行结果:
整个项目的目录:
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"; } }
执行结果:
相关文章推荐
- 基于现如今流行的SSM(spring springMVC mybatis mysql)框架的hrm人事管理系统后台实例
- ssm框架创建简单的DVD管理系统(五)页面的实现同时完成controller层
- 框架之struts2实现简易学生管理系统(struts2文件上传、分层)
- 基于SSM实现的简易员工管理系统
- 【SSH】:基于Struts2+HIbernate3+Spring3实现员工管理系统之框架整合篇
- 简单的.NET三层框架的实现(学生作业管理系统)
- 基于SSM框架的CRM银行管理系统
- 基于python实现学生管理系统
- 基于SSH实现员工管理系统之框架整合篇 学习笔记
- 基于SSM实现的简易员工管理系统(基于阿里云的网站上线篇)
- 基于java+mysql学生管理系统的实现
- 基于配置实现信息管理系统开发基础框架
- 基于SSM实现的简易员工管理系统(基于阿里云的网站上线篇)
- Java基于MySQL实现学生管理系统
- Linux环境下C语言实现简单的基于文件的学生信息管理系统
- (一)基于SSH实现员工管理系统之框架整合篇
- 基于Struts和hibernate框架的学生信息管理系统
- 基于UML和ASP.NET实现三层B/S学生管理系统开发
- 基于SSH实现员工管理系统之框架整合篇
- 基于LitePal操作数据库的学生管理系统的简单实现