本周主要讲了继续对SpringMvc的运用和精致讲解(这里特别感谢赵老师对我的蒟蒻代码的帮助),
主要针对了user构建了userDao层和userServet以及model层
- UserDao
userDao中主要对登陆界面的xml进行拦截,在xml中进行比对然后返回结果(跳转成功或者失败)
<?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.wb.dao.UserDao">
<sql id="sqlWhere">
<where>
<if test="username!=null and username!=''">
and username=#{username}
<!-- Auto delete the first and -->
</if>
<if test="pwd!=null and pwd!=''">
and pwd = #{pwd}
</if>
<if test="realname!=null and realname!=''">
and realname like concat('%',#{realname},'%')
<!--mysql's squence string -->
</if>
</where>
</sql>
<select id="list" parameterMap="user" resultType="user">
select * from user
<include refid="sqlWhere"></include>
</select>
<insert id="create" parameterType="user">
insert into
user(username,pwd,realname)
values(#{username},#{pwd},#{realname})
</insert>
<!--charu -->
<update id="update" parameterType="user">
update user
<set>
<if test="username!=null and username!=''">
username = #{username},
</if>
<if test="pwd!=null and pwd!=''">
pwd = #{pwd},
</if>
<if test="realname!=null and realname!=''">
realname = #{realname},
</if>
</set>
where id = #{id}
</update>
<delete id="delete" parameterType="integer">
delete from user where id =
#{id}
</delete>
<!--批量处理初始化 -->
<update id="updateBatch" parameterType="list">
update user set pwd='123' where id in
<foreach item="item" index="index" collection="list" open="("
separator="," close=")">
#{item}
</foreach>
</update>
</mapper>
这里的几个主要标签需要认识一下,毕竟和html和java都不一样:
select标签:权限系统中,几个常见的业务,需要查询出系统中的用户、角色、权限等数据, 纯JDBC时,需要写查询语句,并且对结果集进行手工处理,将结果映射到对象的属性中。 而如果使用Mybatis,只需要在XML中添加一个select元素,写一个SQL,做一些简单的配置,就可以将结果集映射到对象中。
where标签和if标签搭配使用类似于c++中的循环遍历的条件查询(where if)
mapper则类似于像c++中的命名空间一样
MyBatis 的真正强大在于它的映射语句,这是它的魔力所在。由于它的异常强大,映射器的 XML 文件就显得相对简单。如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立即发现省掉了将近 95% 的代码。MyBatis 为聚焦于 SQL 而构建,以尽可能地为你减少麻烦。
- Service
- model
model主要用于user的实例化
对xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>shop</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置加载Spring文件的监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止中文乱码,过滤器 -->
<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>
<servlet>
<!-- 前端控制器 -->
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化加载配置文件 可以不配,不配时,默认加载的springmvc的配置文件,去WEB-INF下面找:servlet的名称-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
<!-- 表示容器一启动就加载servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
<!-- /拦截所有的请求,*.do拦截访问请求地址后缀是.do的 -->
</servlet-mapping>
</web-app>
下节课希望我的500能跑通!!!!