基于SSM框架学生管理系统的实现
来源:互联网 发布:中国移动iptv网络电视 编辑:程序博客网 时间:2024/06/06 08:50
这是我一年前做的项目的,算是SSM框架入门的实例,maven管理工具所用到的仓库私服为阿里云的仓库。
整个项目的目录:
jdbc.properties是对数据库信息的配置:
#mysql version database settingjdbc.driver=com.mysql.jdbc.Driverjdbc.username=rootjdbc.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;@Servicepublic 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"; }}
执行结果:
阅读全文
0 0
- 基于SSM框架学生管理系统的实现
- 基于ssm框架的绩效管理系统
- 基于JAVA集合框架实现一个简单的学生管理系统
- SSM框架基于swagger2实现API管理
- 基于maven管理的SSM框架搭建
- 基于SSM框架的日志管理
- 基于SSM-EasyUI的权限管理系统
- 基于SSM的健身管理系统
- 基于ssm的场地管理系统
- ssm框架的缺陷管理系统
- 基于SSM实现的简易员工管理系统(基于阿里云的网站上线篇)
- 基于SSM实现的简易员工管理系统(基于阿里云的网站上线篇)
- 基于SSM实现的简易员工管理系统(基于阿里云的网站上线篇)
- 基于数据库的学生管理系统
- 基于C语言的学生管理系统
- 基于pickle模块的学生管理系统
- SSM学生管理系统SpringMVC+MyBatis
- ssm框架创建简单的DVD管理系统(五)页面的实现同时完成controller层
- idea中get\set方法快捷添加
- struts添加验证码
- .NET Core 常用加密和Hash工具NETCore.Encryp
- IP地址和端口
- 清理Xcode里多余的证书配置文件
- 基于SSM框架学生管理系统的实现
- java定时器时间配置
- python的os模块
- 规则绝对公平时,社会财富的流向谁?
- 科沃斯地宝DE35怎么样?
- PHP团队 编码规范 & 代码样式风格规范
- jquery 获取ckeditor值
- Duilib学习之仿酷狗开发(1)
- cookie和session区别