发表于: 2019-12-03 17:46:17
1 985
今天做了什么
编写出了SSM的基本样例,和简陋的JSP页面
目录如下:
Controller
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/allStudent")
public String list(Model model) {
List<Student> list = studentService.queryAllPaper();
model.addAttribute("list", list);
return "allStudent";
}
@RequestMapping("toAddStudent")
public String toAddStudent() {
return "addStudent";
}
@RequestMapping("/addStudent")
public String addPaper(Student student) {
System.out.println("111111111"+student.toString());
studentService.addStudent(student);
return "redirect:/student/allStudent";
}
@RequestMapping("/del/{studentId}")
public String deletePaper(@PathVariable("studentId") Long id) {
System.out.println("++++++++++++++++++++开始了============================================");
studentService.deleteStudentById(id);
System.out.println("++++++++++++++++++++结束了============================================");
return "redirect:/student/allStudent";
}
@RequestMapping("toUpdateStudent")
public String toUpdatePaper(Model model, Long id) {
model.addAttribute("student", studentService.queryById(id));
return "updateStudent";
}
@RequestMapping("/updateStudent")
public String updatePaper(Model model, Student student) {
studentService.updateStudent(student);
student = studentService.queryById(student.getId());
model.addAttribute("paper", student);
return "redirect:/student/allStudent";
}
}
dao层
mapper文件
<?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.dao.StudentDao">
<resultMap id="BaseResultMap" type="com.pojo.Student">
<id column="id" property="id" />
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="qq" property="qq" />
<result column="jnshuType" property="jnshutype" />
<result column="joinTime" property="jointime" />
<result column="school" property="school" />
<result column="onlineId" property="onlineid" />
<result column="dailyUrl" property="dailyurl" />
<result column="slogan" property="slogan" />
<result column="counsellor" property="counsellor" />
<result column="knownPath" property="knownpath" />
<result column="createTime" property="createtime" />
<result column="updateTime" property="updatetime" />
</resultMap>
<sql id="Base_Column_List">
id, name, qq, jnshuType, joinTime, school, onlineId, dailyUrl, slogan, counsellor,
knownPath, createTime, updateTime
</sql>
<insert id="addStudent" parameterType="com.pojo.Student">
insert into student (id, student.name, qq,
jnshuType, joinTime, school,
onlineId, dailyUrl, slogan,
counsellor, knownPath, createTime,
updateTime)
values (#{id}, #{name ,jdbcType=VARCHAR}, #{qq},
#{jnshutype}, #{jointime}, #{school},
#{onlineid}, #{dailyurl}, #{slogan},
#{counsellor}, #{knownpath}, #{createtime},
#{updatetime})
</insert>
<delete id="deleteStudentById" parameterType="java.lang.Long">
DELETE FROM Student WHERE id=#{id}
</delete>
<update id="updateStudent" parameterType="Student">
update student
set name = #{name},
qq = #{qq},
jnshuType = #{jnshutype},
joinTime = #{jointime},
school = #{school},
onlineId = #{onlineid},
dailyUrl = #{dailyurl},
slogan = #{slogan},
counsellor = #{counsellor},
knownPath = #{knownpath},
updateTime = #{updatetime}
where id = #{id,jdbcType=INTEGER}
</update>
<select id="queryById" resultType="Student" parameterType="long">
SELECT
<include refid="Base_Column_List" />
FROM Student
WHERE id=#{id}
</select>
<select id="queryAllStudent" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
FROM Student
</select>
</mapper>
spring-dao.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置整合mybatis过程 -->
<!-- 1.配置数据库相关参数properties的属性:${url} -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2.数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 配置连接池属性 -->
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false"/>
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="10000"/>
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 扫描pojo包 使用别名 -->
<property name="typeAliasesPackage" value="com.pojo"/>
<!-- 扫描sql配置文件:mapper需要的xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.dao"/>
</bean>
</beans>
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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 配置SpringMVC -->
<!-- 1.开启SpringMVC注解模式 -->
<!-- 简化配置:
(1)自动注册DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter
(2)提供一些列:数据绑定,数字和日期的format @NumberFormat, @DateTimeFormat, xml,json默认读写支持
-->
<mvc:annotation-driven />
<!-- 2.静态资源默认servlet配置
(1)加入对静态资源的处理:js,gif,png
(2)允许使用"/"做整体映射
-->
<mvc:default-servlet-handler/>
<!-- 3.配置jsp 显示ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 4.扫描web相关的bean -->
<context:component-scan base-package="com.controller" />
</beans>
spring-service.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 配置SpringMVC -->
<!-- 1.开启SpringMVC注解模式 -->
<!-- 简化配置:
(1)自动注册DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter
(2)提供一些列:数据绑定,数字和日期的format @NumberFormat, @DateTimeFormat, xml,json默认读写支持
-->
<mvc:annotation-driven />
<!-- 2.静态资源默认servlet配置
(1)加入对静态资源的处理:js,gif,png
(2)允许使用"/"做整体映射
-->
<mvc:default-servlet-handler/>
<!-- 3.配置jsp 显示ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 4.扫描web相关的bean -->
<context:component-scan base-package="com.controller" />
</beans>
mybaits-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>
<!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->
<setting name="useGeneratedKeys" value="true" />
<!-- 使用列别名替换列名 默认:true -->
<setting name="useColumnLabel" value="true" />
<!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} -->
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
</configuration>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1" metadata-complete="true">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置springMVC需要加载的配置文件
spring-dao.xml,spring-service.xml,spring-mvc.xml
Mybatis - > spring -> springmvc
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<!-- 默认匹配所有的请求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
JSP页面
运行效果:
基本完成了基本的curd,但是jsp页面并没有完全把属性展示出来
遇到的问题:
1.数据库编码不对,修改数据库的编码为utf-8(表也要修改)
2.在mapper.xml中对编写sql语句时方法,属性没有对上,url路径经常写错
收获:
1.熟悉了JSP的基本使用,JSP中可以引入script标签,编写函数(提交表单),如何在按钮上绑定函数,提交表单
2.了解了整个SSM的运转流程
jsp--表单提交--Controller--service--dao--mapper--数据库
3.在师兄的帮助下,学会了如何解决报错
看报错,锁定问题,打日志,找到原因并解决问题
明天要做什么:
丰富JSP的页面,展列出其他属性
将程序打包到服务器上,使用其他服务器运行(tomcat、jetty、resin)
学习使用Postman
评论