• SSM(Spring SpringMVC MyBatis)配置文件信息,完成学生管理页面(前后端全部代码)


    效果图(elementUI

    项目结构

    web.xml

    1. "1.0" encoding="UTF-8"?>
    2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    5. version="4.0">
    6. <servlet>
    7. <servlet-name>DispatcherServletservlet-name>
    8. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    9. <init-param>
    10. <param-name>contextConfigLocationparam-name>
    11. <param-value>classpath:springmvc.xmlparam-value>
    12. init-param>
    13. <load-on-startup>1load-on-startup>
    14. servlet>
    15. <servlet-mapping>
    16. <servlet-name>DispatcherServletservlet-name>
    17. <url-pattern>/url-pattern>
    18. servlet-mapping>
    19. <filter>
    20. <filter-name>CharacterEncodingFilterfilter-name>
    21. <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    22. <init-param>
    23. <param-name>encodingparam-name>
    24. <param-value>utf-8param-value>
    25. init-param>
    26. filter>
    27. <filter-mapping>
    28. <filter-name>CharacterEncodingFilterfilter-name>
    29. <url-pattern>/*url-pattern>
    30. filter-mapping>
    31. <context-param>
    32. <param-name>contextConfigLocationparam-name>
    33. <param-value>classpath:spring.xmlparam-value>
    34. context-param>
    35. <listener>
    36. <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    37. listener>
    38. <filter>
    39. <filter-name>httpPutFormContentFilterfilter-name>
    40. <filter-class>org.springframework.web.filter.HttpPutFormContentFilterfilter-class>
    41. filter>
    42. <filter-mapping>
    43. <filter-name>httpPutFormContentFilterfilter-name>
    44. <url-pattern>/*url-pattern>
    45. filter-mapping>
    46. web-app>

    springMVC

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:mvc="http://www.springframework.org/schema/mvc"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans
    7. http://www.springframework.org/schema/beans/spring-beans.xsd
    8. http://www.springframework.org/schema/context
    9. http://www.springframework.org/schema/context/spring-context.xsd
    10. http://www.springframework.org/schema/mvc
    11. http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    12. <context:component-scan base-package="com.etime.controller">context:component-scan>
    13. <mvc:annotation-driven>mvc:annotation-driven>
    14. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    15. <property name="prefix" value="/">property>
    16. <property name="suffix" value=".html">property>
    17. bean>
    18. beans>

    spring

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:aop="http://www.springframework.org/schema/aop"
    6. xmlns:tx="http://www.springframework.org/schema/tx"
    7. xsi:schemaLocation="http://www.springframework.org/schema/beans
    8. http://www.springframework.org/schema/beans/spring-beans.xsd
    9. http://www.springframework.org/schema/context
    10. http://www.springframework.org/schema/context/spring-context.xsd
    11. http://www.springframework.org/schema/aop
    12. http://www.springframework.org/schema/aop/spring-aop.xsd
    13. http://www.springframework.org/schema/tx
    14. http://www.springframework.org/schema/tx/spring-tx.xsd">
    15. <context:component-scan base-package="com.etime.service">context:component-scan>
    16. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    17. <property name="basePackage" value="com.etime.dao">property>
    18. bean>
    19. <context:property-placeholder location="classpath:db.properties"/>
    20. <bean id="pool" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    21. <property name="driverClass" value="${jdbc.driver}"/>
    22. <property name="jdbcUrl" value="${jdbc.url}"/>
    23. <property name="user" value="${jdbc.username}"/>
    24. <property name="password" value="${jdbc.password}"/>
    25. bean>
    26. <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    27. <property name="dataSource" ref="pool"/>
    28. <property name="typeAliasesPackage" value="com.etime.pojo"/>
    29. <property name="mapperLocations" value="classpath:com/etime/dao/*.xml"/>
    30. <property name="plugins">
    31. <array>
    32. <bean class="com.github.pagehelper.PageInterceptor">bean>
    33. array>
    34. property>
    35. <property name="configLocation" value="classpath:mybatis.xml">property>
    36. bean>
    37. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    38. <property name="dataSource" ref="pool"/>
    39. bean>
    40. <tx:annotation-driven transaction-manager="transactionManager"/>
    41. beans>

    myBatis

    1. "1.0" encoding="UTF-8"?>
    2. configuration
    3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
    5. <configuration>
    6. <settings>
    7. <setting name="logImpl" value="STDOUT_LOGGING" />
    8. settings>
    9. configuration>

    studentDao.xml

    1. "1.0" encoding="UTF-8"?>
    2. mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.etime.dao.StudentDao">
    6. <select id="getAllStudent" parameterType="Student" resultMap="getAllStudentMap">
    7. SELECT * FROM student s,class c where s.class_id = c.cid
    8. <if test="sname != ''">
    9. and sname like concat('%',#{sname},'%')
    10. if>
    11. <if test="gender != ''">
    12. and gender = #{gender}
    13. if>
    14. <if test="class_id != 0">
    15. and class_id = #{class_id}
    16. if>
    17. select>
    18. <resultMap id="getAllStudentMap" type="Student">
    19. <id property="sid" column="sid">id>
    20. <result property="gender" column="gender">result>
    21. <result property="sname" column="sname">result>
    22. <association property="clazz" javaType="Clazz">
    23. <id property="cid" column="cid">id>
    24. <result property="caption" column="caption">result>
    25. association>
    26. resultMap>
    27. <select id="getAllCourseBySid" parameterType="int" resultMap="getAllCourseBySidMap">
    28. select *
    29. from score s,
    30. course c,
    31. teacher t
    32. where c.cid = s.course_id
    33. and c.teacher_id = t.tid
    34. and s.student_id = #{sid}
    35. select>
    36. <resultMap id="getAllCourseBySidMap" type="Score">
    37. <id property="sid" column="sid">id>
    38. <result property="student_id" column="student_id">result>
    39. <result property="course_id" column="course_id">result>
    40. <result property="num" column="num">result>
    41. <collection property="courses" ofType="Course">
    42. <id property="cid" column="cid">id>
    43. <result property="cname" column="cname">result>
    44. <result property="teacher_id" column="teacher_id">result>
    45. <association property="teacher" javaType="Teacher">
    46. <id property="tid" column="tid">id>
    47. <result property="tname" column="tname">result>
    48. association>
    49. collection>
    50. resultMap>
    51. <delete id="deleteStudents">
    52. delete from student where sid in
    53. <foreach collection="array" item="sid" separator="," open="(" close=")">
    54. #{sid}
    55. foreach>
    56. delete>
    57. mapper>

    studentDao

    1. @Repository
    2. public interface StudentDao {
    3. List getAllStudent(Student student);
    4. @Select("select * from class")
    5. List getAllClass();
    6. List getAllCourseBySid(int sid);
    7. int deleteStudents(int[] sids);
    8. @Insert("insert into student(gender,class_id,sname) values(#{gender},#{class_id},#{sname})")
    9. int addStudent(Student student);
    10. @Update("update student set sname = #{sname},class_id=#{class_id},gender=#{gender} where sid = #{sid}")
    11. int editStudent(Student student);
    12. }

    studentService

    1. @Service
    2. public class StudentServiceImpl implements StudentService {
    3. @Autowired
    4. private StudentDao studentDao;
    5. @Override
    6. public PageInfo getAllStudent(int page,int rows,Student student) {
    7. PageHelper.startPage(page,rows);
    8. List list = studentDao.getAllStudent(student);
    9. PageInfo info = new PageInfo<>(list);
    10. System.out.println(info);
    11. return info;
    12. }
    13. @Override
    14. public List getAllCourseBySid(int sid) {
    15. return studentDao.getAllCourseBySid(sid);
    16. }
    17. @Override
    18. public List getAllClass() {
    19. return studentDao.getAllClass();
    20. }
    21. @Override
    22. public boolean deleteStudents(int[] sids) {
    23. return studentDao.deleteStudents(sids)==0?false:true;
    24. }
    25. @Override
    26. public boolean addStudent(Student student) {
    27. return studentDao.addStudent(student) == 0?false:true;
    28. }
    29. @Override
    30. public boolean editStudent(Student student) {
    31. return studentDao.editStudent(student)==0?false:true;
    32. }
    33. }

    controller

    1. @Controller
    2. @RequestMapping("student")
    3. @ResponseBody
    4. @CrossOrigin
    5. public class StudentController {
    6. @Autowired
    7. private StudentService studentService;
    8. /*分页展示加搜索*/
    9. @GetMapping("getAllStudent")
    10. public PageInfo getAllStudent(int page, int rows, String sname, String gender, String cid) {
    11. int cids;
    12. if (cid == null || cid == "") {
    13. cids = 0;
    14. } else {
    15. cids = Integer.parseInt(cid);
    16. }
    17. System.out.println(sname + "," + gender);
    18. return studentService.getAllStudent(page, rows, new Student(gender, cids, sname));
    19. }
    20. /*查所有班级*/
    21. @GetMapping("getAllClass")
    22. public List getAllClass(){
    23. return studentService.getAllClass();
    24. }
    25. /*查询学生所有课程信息*/
    26. @GetMapping("getAllCourseBySid/{sid}")
    27. public List getAllCourseBySid(@PathVariable("sid")int sid) {
    28. return studentService.getAllCourseBySid(sid);
    29. }
    30. /*删除*/
    31. @DeleteMapping("deleteStudents")
    32. public boolean deleteStudents(@RequestBody int[] sids){
    33. return studentService.deleteStudents(sids);
    34. }
    35. /*添加*/
    36. @PostMapping("addStudent")
    37. public boolean addStudent(@RequestBody Student student){
    38. return studentService.addStudent(student);
    39. }
    40. /*修改*/
    41. @PutMapping("editStudent")
    42. public boolean editStudent(@RequestBody Student student){
    43. return studentService.editStudent(student);
    44. }

    index.html

    1. <head>
    2. <title>title>
    3. <meta charset="UTF-8">
    4. <link rel="stylesheet" href="element-ui-2.13.0/lib/theme-chalk/index.css" />
    5. <script type="text/javascript" src="vue/vue-v2.6.10.js">script>
    6. <script type="text/javascript" src="element-ui-2.13.0/lib/index.js">script>
    7. <script type="text/javascript" src="vue/axios-0.18.0.js">script>
    8. head>
    9. <body>
    10. <div id="app">
    11. <template>
    12. <el-table :data="tableData" @selection-change="handleSelectionChange" size="medium"
    13. highlight-current-row="true" style="width: 100%">
    14. <el-table-column type="selection" width="55" prop="sid">
    15. el-table-column>
    16. <el-table-column width="100px" label="序号" type="index">
    17. el-table-column>
    18. <el-table-column label="姓名" prop="sname">
    19. el-table-column>
    20. <el-table-column label="性别" prop="gender">
    21. el-table-column>
    22. <el-table-column label="班级" prop="clazz.caption">
    23. el-table-column>
    24. <el-table-column>
    25. <template slot="header" slot-scope="scope">
    26. <el-input v-model="search" placeholder="请输入姓名" />
    27. template>
    28. el-table-column>
    29. <el-table-column>
    30. <template slot="header" slot-scope="scope">
    31. <el-select v-model="cid" placeholder="请选择班级">
    32. <el-option v-for="item in classes" :key="item.cid" :label="item.caption" :value="item.cid">
    33. el-option>
    34. el-select>
    35. template>
    36. el-table-column>
    37. <el-table-column>
    38. <template slot="header" slot-scope="scope">
    39. <el-radio v-model="sex" label="男">el-radio>
    40. <el-radio v-model="sex" label="女">el-radio>
    41. template>
    42. <template slot-scope="scope">
    43. <el-button size="mini" @click="handleLook(scope.$index, scope.row)">查看课程信息el-button>
    44. template>
    45. el-table-column>
    46. <el-table-column>
    47. <template slot="header" slot-scope="scope">
    48. <el-button type="success" @click="findAll()">搜索el-button>
    49. template>
    50. <template slot-scope="scope">
    51. <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">修改el-button>
    52. template>
    53. el-table-column>
    54. el-table>
    55. template>
    56. <br />
    57. <el-row>
    58. <el-button type="warning" @click="delAll()">删除选中el-button>
    59. <el-button type="primary" @click="add()">添加用户el-button>
    60. el-row>
    61. <template>
    62. <div class="block" align="right">
    63. <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
    64. :current-page="currentPage" :page-sizes="[3, 4, 5, 6, 7, 8]" :page-size="pageSize"
    65. layout="total, sizes, prev, pager, next, jumper" :total="totalCount">
    66. el-pagination>
    67. div>
    68. template>
    69. <el-dialog title="查看课程信息" :visible.sync="dialogFormVisible">
    70. <el-form ref="ruleForm" :model="ruleForm" label-width="80px">
    71. <el-form-item label="学生姓名">
    72. <el-input v-model="ruleForm.sname" style="width: 210px;" readonly>el-input>
    73. el-form-item>
    74. el-form>
    75. <el-table :data="tableCourse" @selection-change="handleSelectionChange" size="medium"
    76. highlight-current-row="true" style="width: 100%">
    77. <el-table-column width="100px" label="序号" type="index">
    78. el-table-column>
    79. <el-table-column label="课程" prop="courses[0].cname">
    80. el-table-column>
    81. <el-table-column label="成绩" prop="num">
    82. el-table-column>
    83. <el-table-column label="老师" prop="courses[0].teacher.tname">
    84. el-table-column>
    85. el-table>
    86. el-dialog>
    87. <el-dialog title="添加学生信息" :visible.sync="diaAdd">
    88. <el-form :model="ruleForm" ref="ruleForm" label-width="100px">
    89. <el-form-item label="姓名" prop="sname">
    90. <el-input v-model="ruleForm.sname" style="width: 210px;">el-input>
    91. el-form-item>
    92. <el-form-item label="性別" prop="gender">
    93. <el-radio-group v-model="ruleForm.gender">
    94. <el-radio label="男">el-radio>
    95. <el-radio label="女">el-radio>
    96. el-radio-group>
    97. el-form-item>
    98. <el-form-item label="班级" prop="class_id">
    99. <el-select v-model="ruleForm.class_id" placeholder="请选择班级">
    100. <el-option v-for="item in classes" :key="item.cid" :label="item.caption" :value="item.cid">
    101. el-option>
    102. el-select>
    103. el-form-item>
    104. <el-form-item>
    105. <el-button type="primary" @click="submitForm()">立即添加el-button>
    106. el-form-item>
    107. el-form>
    108. el-dialog>
    109. <el-dialog title="修改学生信息" :visible.sync="dialogVisible">
    110. <el-form :model="ruleForm" ref="ruleForm" label-width="100px">
    111. <el-form-item label="姓名" prop="sname">
    112. <el-input v-model="ruleForm.sname" style="width: 210px;">el-input>
    113. el-form-item>
    114. <el-form-item label="性別" prop="gender">
    115. <el-radio-group v-model="ruleForm.gender">
    116. <el-radio label="男">el-radio>
    117. <el-radio label="女">el-radio>
    118. el-radio-group>
    119. el-form-item>
    120. <el-form-item label="班级" prop="class_id">
    121. <el-select v-model="ruleForm.clazz.cid" placeholder="请选择班级">
    122. <el-option v-for="item in classes" :key="item.cid" :label="item.caption" :value="item.cid">
    123. el-option>
    124. el-select>
    125. el-form-item>
    126. <el-form-item>
    127. <el-button type="primary" @click="submitFormEd()">立即修改el-button>
    128. el-form-item>
    129. el-form>
    130. el-dialog>
    131. div>
    132. body>
    133. <script>
    134. axios.defaults.withCredentials = false
    135. new Vue({
    136. el: "#app",
    137. data: {
    138. /*表格数据*/
    139. tableData: [],
    140. tableCourse: [],
    141. /*条件查询关键字*/
    142. search: '',
    143. sex: "",
    144. //批量删除存放选中的复选框
    145. multipleSelection: [],
    146. //存放删除的数据
    147. delarr: [],
    148. //当前页
    149. currentPage: 1,
    150. //每页显示条数
    151. pageSize: 5,
    152. //总条数
    153. totalCount: '',
    154. //总页数
    155. totalPage: '',
    156. // 是否展示课程信息对话框
    157. dialogFormVisible: false,
    158. diaAdd: false,
    159. dialogVisible: false,
    160. ruleForm: {
    161. sid: '',
    162. sname: '',
    163. gender: '',
    164. clazz: '',
    165. class_id: '',
    166. cid: '',
    167. },
    168. classes: '',
    169. cid: '',
    170. },
    171. methods: {
    172. findAll() {
    173. axios({
    174. method: "get",
    175. url: "http://localhost:8080/day11_war_exploded/student/getAllStudent",
    176. params: {
    177. page: this.currentPage,
    178. rows: this.pageSize,
    179. sname: this.search,
    180. gender: this.sex,
    181. cid: this.cid
    182. }
    183. }).then(obj => {
    184. console.log(obj)
    185. this.tableData = obj.data.list;
    186. this.totalCount = obj.data.total;
    187. });
    188. },
    189. getAllClass() {
    190. axios({
    191. method: "get",
    192. url: "http://localhost:8080/day11_war_exploded/student/getAllClass",
    193. }).then(obj => {
    194. this.classes = obj.data
    195. });
    196. },
    197. handleSizeChange: function (size) {
    198. this.pageSize = size;
    199. this.findAll();
    200. },
    201. handleCurrentChange: function (currentPage) {
    202. this.currentPage = currentPage;
    203. this.findAll();
    204. },
    205. // 详情
    206. handleLook(index, row) {
    207. this.dialogFormVisible = true
    208. this.ruleForm = row
    209. axios({
    210. method: "get",
    211. url: "http://localhost:8080/day11_war_exploded/student/getAllCourseBySid/" + row.sid,
    212. }).then(obj => {
    213. this.tableCourse = obj.data
    214. });
    215. },
    216. delAll() {
    217. //获取删除的ID
    218. this.delarr = [];
    219. for (let i = 0; i < this.multipleSelection.length; i++) {
    220. this.delarr.push(this.multipleSelection[i].sid);
    221. }
    222. //判断要删除的文件是否为空
    223. if (this.delarr.length == 0) {
    224. this.$message.warning("请选择要删除的数据!")
    225. } else {
    226. this.$confirm("是否确认删除?", "提示", { type: 'warning' }).then(() => {
    227. //点击确认删除
    228. axios({
    229. method: "delete",
    230. url: "http://localhost:8080/day11_war_exploded/student/deleteStudents",
    231. data: this.delarr
    232. }).then(obj => {
    233. if (obj.data) {
    234. this.$message.success("删除成功");
    235. } else {
    236. this.$message.console.error("删除失败");
    237. }
    238. this.findAll();
    239. });
    240. });
    241. }
    242. },
    243. handleSelectionChange(val) {
    244. this.multipleSelection = val;
    245. },
    246. // 添加
    247. add() {
    248. this.diaAdd = true;
    249. },
    250. submitForm() {
    251. axios({
    252. method: "post",
    253. url: "http://localhost:8080/day11_war_exploded/student/addStudent",
    254. data: {
    255. sname: this.ruleForm.sname,
    256. gender: this.ruleForm.gender,
    257. class_id: this.ruleForm.class_id
    258. }
    259. }).then(obj => {
    260. if (obj.data) {
    261. this.$message.success("添加成功");
    262. } else {
    263. this.$message.error("添加失败");
    264. }
    265. this.findAll();
    266. });
    267. },
    268. // 修改
    269. handleEdit(index, row) {
    270. this.dialogVisible = true;
    271. this.ruleForm = row;
    272. },
    273. submitFormEd() {
    274. axios({
    275. method: "put",
    276. url: "http://localhost:8080/day11_war_exploded/student/editStudent",
    277. data: {
    278. sname: this.ruleForm.sname,
    279. gender: this.ruleForm.gender,
    280. class_id: this.ruleForm.clazz.cid,
    281. sid:this.ruleForm.sid
    282. }
    283. }).then(obj => {
    284. if (obj.data) {
    285. this.$message.success("修改成功");
    286. } else {
    287. this.$message.error("修改失败");
    288. }
    289. this.findAll();
    290. });
    291. },
    292. },
    293. created() {
    294. this.findAll();
    295. this.getAllClass();
    296. }
    297. })
    298. script>
    299. html>

  • 相关阅读:
    【Linux网络】网卡配置与修改主机名,做好基础系统配置
    【5G PHY】物理层逻辑和物理天线的映射
    C/C++之数据结构与算法2:高精度计算-减法
    【自然语言处理(NLP)】基于注意力机制的中-英机器翻译
    中科大给师生们发了一封钓鱼邮件 结果3000多人上当了
    Iis7.0-7.5 fast-cgi解析漏洞
    Java常用类和对象---尚硅谷Java入门视频学习
    在屏幕上打印杨辉三角
    推荐系统(RS)
    CTF-PUT上传漏洞【超详细】
  • 原文地址:https://blog.csdn.net/weixin_62971115/article/details/133993290