02-Maven spring springmvc mybati

2017-12-28  本文已影响10人  CoderLJW

看过01的朋友,接下来请看这里,以图开篇
记得Eclipse经常出问题,要时常

-- domain代码

package com.loujianwei.domain;

public class User {
    private Integer id;

    private String userName;

    private String password;

    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName == null ? null : userName.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

--dao代码

package com.loujianwei.dao;

import com.loujianwei.domain.User;

public interface UserDao {
    int deleteByPrimaryKey(Integer id);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
}

--service代码

package com.loujianwei.service;

import com.loujianwei.domain.User;

public interface UserService {
        int deleteByPrimaryKey(Integer id);

        int insert(User record);

        int insertSelective(User record);

        User selectByPrimaryKey(Integer id);

        int updateByPrimaryKeySelective(User record);

        int updateByPrimaryKey(User record);
}

--serviceImpl代码

package com.loujianwei.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.loujianwei.dao.UserDao;
import com.loujianwei.domain.User;
import com.loujianwei.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    public int deleteByPrimaryKey(Integer id) {
        // TODO Auto-generated method stub
        return userDao.deleteByPrimaryKey(id);
    }

    public int insert(User record) {
        return userDao.insert(record);
    }

    public int insertSelective(User record) {
        // TODO Auto-generated method stub
        return userDao.insertSelective(record);
    }

    public User selectByPrimaryKey(Integer id) {
        // TODO Auto-generated method stub
        return userDao.selectByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(User record) {
        // TODO Auto-generated method stub
        return userDao.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(User record) {
        return userDao.updateByPrimaryKey(record);
    }

}

--mapper.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.loujianwei.dao.UserDao" >
  <resultMap id="BaseResultMap" type="com.loujianwei.domain.User" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="user_name" property="userName" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
    <result column="age" property="age" jdbcType="INTEGER" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, user_name, password, age
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from user_t
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from user_t
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.loujianwei.domain.User" >
    insert into user_t (id, user_name, password, 
      age)
    values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
      #{age,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="com.loujianwei.domain.User" >
    insert into user_t
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="userName != null" >
        user_name,
      </if>
      <if test="password != null" >
        password,
      </if>
      <if test="age != null" >
        age,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="userName != null" >
        #{userName,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="age != null" >
        #{age,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.loujianwei.domain.User" >
    update user_t
    <set >
      <if test="userName != null" >
        user_name = #{userName,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        password = #{password,jdbcType=VARCHAR},
      </if>
      <if test="age != null" >
        age = #{age,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.loujianwei.domain.User" >
    update user_t
    set user_name = #{userName,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      age = #{age,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

使用测试类,测试mybatis功能 先上代码

package com.loujianwei.gryun;
// spring框架使用的也是这个注解
import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
// 用于序列化 好看
import com.alibaba.fastjson.JSON;

import com.loujianwei.service.UserService;

@RunWith(SpringJUnit4ClassRunner.class) // 表示继承了SpringJUnit4ClassRunner类
@ContextConfiguration(locations = "classpath:spring-mybatis.xml")
public class TestGryun {
    private static Logger logger = Logger.getLogger(TestGryun.class);
    @Resource
    private UserService userService;
    @Test
    public void testDao() {
        System.out.println("aa");
        int aa = userService.deleteByPrimaryKey(1);
        logger.info(JSON.toJSONString(aa));
    }
}

测试错误

重要的是看Caused by 和 最上面的那个红线那里,大概就可以看出来问题,因为是class path resource xxxxx.xml not exist看到这里答案就出来了。

1、 图片.png
修改错误地方为
@ContextConfiguration(locations = "classpath:conf/spring-mybatis.xml")

2、看着像是缺少了ibatis包,加上后还是出现这个错误,最后检测路径正确但是创建不成功,有可能是jar包缺少。最后检测缺少了mybatis:3.2.8的包


图片.png

启动tomcat 测试 spring-mvc和spring的整合

1、启动tomcat(把项目加入tomcat中运行)前,需要确保Maven Install了,运行Maven Install会有一些错误如

3、又遇见这个问题,唯一高兴的就是 tomcat启动成功了

package com.loujianwei.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class LoginController {
    @RequestMapping("/login.do")
    public String login() {
        return "login";
    }
}

浏览器输入

http://localhost:8080/gryun/login.do

返回界面


图片.png

记录使用中的一个Maven现象
项目【右键】--【Maven】--【Update Project】时,
1、会把【WEB-INF/lib】文件夹下的jar包清空。
2、还有如图的地方也会消失,需要自己再次加上去。不晓的有没有可以省略这里配置的


图片.png

3、会遇见Maven中的jar包没有完全加入到lib目录下。可以手动加入,也可以使用上面的 1---2步骤来完成

上一篇 下一篇

猜你喜欢

热点阅读