搭建项目经验第二阶段

上一篇的经验主要是搭框架,这一次将会是一些比较实质的内容啦。
10、开始xml的编写,首先将xml的namespace指向对应的dao。先写简单的增删改查语句,如果是编写update和delete语句,建议不要用where标签,以免在没有注入条件时,把整个表都改了。以下是查询和新增语句的样例,基本上很多功能都涵盖了。

<?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="cn.dw.hz.dao.UserMapper">
    <resultMap type="cn.dw.hz.model.User" id="resultMap">
        <result column="login_id" property="loginId"/>
        <result column="register_time" property="registerTime"/>
    </resultMap>
<select id="query" parameterType="cn.dw.hz.dto.UserDto" resultMap="resultMap">
select * from user
<where>
<if test="name!=null and name !=''">
and name = #{name}
</if>
<if test="loginId !=null and loginId !=''">
and login_id = #{loginId}
</if>
</where>
<if test="pyl !=null and myl !=null">
LIMIT #{pyl},#{myl}
</if>
</select>

<insert id="add" parameterType="cn.dw.hz.model.User" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
insert into user 
<trim prefix="(" suffix=")" suffixOverrides=",">
            <!-- 只要字符串类型才需要加上 !='' 的判断 -->
            <if test="name !=null and name !='' ">
                name,
            </if>
            <if test="loginId !=null and loginId !='' ">
                <!-- 里面对应的是数据库表的字段名称 -->
                login_id,
            </if>
            <if test="sex !=null">
                sex,
            </if>
            <if test="age !=null">
                age,
            </if>
            <if test="password !=null and password !='' ">
                password,
            </if>
            <if test="registerTime !=null">
                register_time,
            </if>
            <if test="status !=null">
                status,
            </if>

        </trim>
        values 
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="name !=null and name !='' ">#{name},
</if>
<if test="loginId !=null and loginId !='' ">#{loginId},
</if>
<if test="sex !=null">#{sex},
</if>
<if test="age !=null">#{age},
</if>
<if test="password !=null and password !=''">#{password},
</if>
<if test="registerTime !=null">#{registerTime},
</if>
<if test="status !=null">#{status},
</if>
</trim>
</insert>

11、开始impl的编写,先把dao类作为 一个对象放入impl,并设置自动注入托管,主要是在实现方法里加入dao类的执行,@Service 和 @Autowired。如果当前serviceimpl需要对其他类的数据库进行操作,那就得把目标类对应的mapper也作为对象放入并自动托管。

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void add(User user) {
        userMapper.add(user);

    }
}

12、开始controller的编写,先把service作为一个对象放入controller,并设置自动注入托管,加上@Controller @Autowired 。利用@RequestMapping 设置对地址的匹配。注意虽然依赖的是service,但容器会自动将serviceimpl注入,实际上用的就是serviceimpl的方法。

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
}

13、打开web.xml,配置DispatcherServlet的servlet和servlet mapping节点,注意设置好配置文件路径。另外要注意开放静态资源拦截,以保证静态js资源可以使用,例如日期插件和jquery。如下面的配置,所有带/的路径均会被DispatcherServlet处理,但如果放在static文件夹下的内容(包括各类css、js)就不会被DispatcherServlet处理。

  <servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:application.xml</param-value>
  </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>
  </servlet-mapping>
   <servlet-mapping>
  <servlet-name>default</servlet-name>
  <url-pattern>/static/*</url-pattern>
 </servlet-mapping>

14、利用eclipse的spring bean configxxxx模板新建配置DispatcherServlet文件,打开DispatcherServlet的配置文件写配置信息。不采用传统方式而采用包扫描注解的方式管理bean,所以点击namespace标签,选择加载context头文件,启用context的标签。然后加上context:component-scan标签并设置base-package表示扫描这个包里的所有注解。

<context:component-scan base-package="cn.dw.hz"></context:component-scan>

好了,我们可以写一个小的测试样例试一下。以下代码放在UserController 里。然后使用地址http://localhost:8080/kfjs2/user/test访问。如果eclipse里控制台显示“有人在测试”且浏览器里显示“ok”。则算是测试成功啦!

    @RequestMapping("/test")
    @ResponseBody
    public String test()
    {
        System.out.println("有人在测试");
        return "ok";
    }

猜你喜欢

转载自blog.csdn.net/cdfabc/article/details/82585136
今日推荐