看过01的朋友,接下来请看这里,以图开篇
记得Eclipse经常出问题,要时常
- clean项目
- clean tomcat
- clean maven
-
更要记得 项目【右键】--【Maven】--【Update Project】
图片.png
使用MyBatis Generator自动创建代码的代码放到相应的位置即可
- mybatis是不用创建xxxDaoImpl类的,他的实现在xxxMapper.xml里面
- 在spring-mybatis.xml配置文件中需要修改文件位置,如果和我的不同
-- 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看到这里答案就出来了。
修改错误地方为
- classpath 加载的路径是类路径下 也就是【WEB-INF/classes】目录下
@ContextConfiguration(locations = "classpath:conf/spring-mybatis.xml")
2、看着像是缺少了ibatis包,加上后还是出现这个错误,最后检测路径正确但是创建不成功,有可能是jar包缺少。最后检测缺少了mybatis:3.2.8的包
启动tomcat 测试 spring-mvc和spring的整合
1、启动tomcat(把项目加入tomcat中运行)前,需要确保Maven Install了,运行Maven Install会有一些错误如
- 这个是路径错误,检查配置文件,修改对应位置
-
如果还错,可以可以看到这里是test代码引起的错误,先注释试试
图片.png
2、Maven Install 运行出来的错误没有了,接下来启动tomcat
- 错误 Error configuring application listener of,查找原因吧
- 也可以尝试重启Eclipse
-
查看这里
图片.png -
还有这里
图片.png -
还有这里 看到了这里有个问题 缺少了 Maven管理的jar包位置
图片.png -
应该这样配置
图片.png - 还有这里
记得Eclipse经常出问题,要时常 - clean项目
- clean tomcat
- clean maven
-
更要记得 项目【右键】--【Maven】--【Update Project】
图片.png
如果上面几个都没有解决,那你再找找度娘
3、又遇见这个问题,唯一高兴的就是 tomcat启动成功了
- Cannot find class [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter] 这个东西没有加入或者没有找到
- 可以确认下次路径在有没有 lib文件夹和里面有没有缺少的jar包
/Users/paycloud110/eclipse-workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/gryun -
缺少了jar包也可以手动添加进来
图片.png
4、测试controller是否成功
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
返回界面
记录使用中的一个Maven现象
项目【右键】--【Maven】--【Update Project】时,
1、会把【WEB-INF/lib】文件夹下的jar包清空。
2、还有如图的地方也会消失,需要自己再次加上去。不晓的有没有可以省略这里配置的
3、会遇见Maven中的jar包没有完全加入到lib目录下。可以手动加入,也可以使用上面的 1---2步骤来完成