发表于: 2018-01-14 13:31:29
2 751
今天完成的事情
1.今天整合mybatis和spring,遇到了一些问题
在这都说一下什么问题:首先是配置完了之后说找不到配置文件Student.xml,可是我已经将这个问价放到了resource文件夹下了啊,于是就开始各种找啊找啊,最后在师兄帮助下找到一篇博客,记录了类似的问题;解决方法:在resource文件夹下新建包mapper,将上面的xml文件移进去,然后,在applocationContext.xml文件中添加上下面的代码,就好了。
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
第二个问题是在进行单元测试的时候报错:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
解决的方法就是:一般的原因是Mapper interface和xml文件的定义对应不上,需要检查包名,namespace,函数名称等能否对应上,需要比较细致的对比
我就仔细检查了自己对应的包名,发现是名称打错了一个字母。
第三个问题就是上面的错误解决之后,单元测试方法的update方法测试不成功,我的方法写的是根据id来修改名称,死活不成功,后来请教虎子哥,虎子哥告诉我因为我的方法是传入两个参数,系统不知道怎么取,传不过去,用到了@Param注解。
public void updateStudent( @Param("student")Student student, @Param("id") int id);
@Test
public void updateStudent() {
s = studentMapper.getStudent(35);
s.setName("lalala");
studentMapper.updateStudent(s,35);
}
这样就可以了。
接下来传一下项目目录:
这是完成的醒目目录
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.student</groupId>
<artifactId>SpringMybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringMybatis</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
mapper类
package com.student.mapper;
import com.student.pojo.Student;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface StudentMapper {
public void addStudent(Student student);
public void deleteStudent(int id);
public void updateStudent( @Param("student")Student student, @Param("id") int id);
public Student getStudent(int id);
public List<Student> listStudent();
}
pojo类:
package com.student.pojo;
public class Student {
private int id;
private String name;
private int qq;
private String school;
private String type;
private String say;
private int create_at;
private int update_at;
public Student(int id, String name, int qq, String school, String type, String say, int create_at, int update_at) {
this.id = id;
this.name = name;
this.qq = qq;
this.school = school;
this.type = type;
this.say = say;
this.create_at = create_at;
this.update_at = update_at;
}
public Student() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQq() {
return qq;
}
public void setQq(int qq) {
this.qq = qq;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSay() {
return say;
}
public void setSay(String say) {
this.say = say;
}
public int getCreate_at() {
return create_at;
}
public void setCreate_at(int create_at) {
this.create_at = create_at;
}
public int getUpdate_at() {
return update_at;
}
public void setUpdate_at(int update_at) {
this.update_at = update_at;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", qq=" + qq +
", school='" + school + '\'' +
", type='" + type + '\'' +
", say='" + say + '\'' +
", create_at=" + create_at +
", update_at=" + update_at +
'}';
}
}
student.xml 相当于dao的实现类:
<?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.student.mapper.StudentMapper"> //绑定dao接口,否则还要写一个实现类或者叫主业务逻辑
<insert id="addStudent" parameterType="Student">
insert into db(id,name,qq,school,type,say,create_at,update_at) values(#{id},#{name},#{qq},#{school},#{type},#{say},#{create_at},#{update_at})
</insert>
<delete id="deleteStudent" parameterType="Student">
delete from db where id = #{id}
</delete>
<select id="getStudent" parameterType="_int" resultType="Student">
select * from db where id = #{id}
</select>
<update id="updateStudent" parameterType="Student">
update db set name = #{student.name} where id = #{id}
</update>
<select id="listStudent" resultType="Student">
select * from db
</select>
</mapper>
spring配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/student?characterEncoding=UTF-8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.student.pojo" />
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.student.mapper"/>
</bean>
</beans>
测试类:
package com.student;
import com.student.mapper.StudentMapper;
import com.student.pojo.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class mainTest{
Student s = new Student();
@Autowired
private StudentMapper studentMapper;
@Test
public void addStudent() {
s.setName("xiao");
s.setQq(111);
s.setSchool("s77星云");
s.setType("jj");
s.setSay("xixixixi");
s.setCreate_at(1988);
s.setUpdate_at(1996);
studentMapper.addStudent(s);
}
@Test
public void testDelete() {
studentMapper.deleteStudent(8);
}
@Test
public void getStudent() {
Student s1 = studentMapper.getStudent(3);
System.out.println(s1);
}
@Test
public void updateStudent() {
s = studentMapper.getStudent(35);
s.setName("lalala");
studentMapper.updateStudent(s,35);
}
@Test
public void eee(){
System.out.println("-----------------------");
}
@Test
public void testList(){
List<Student> ss = studentMapper.listStudent();
for (Student s : ss){
System.out.println(s.getName());
}
}
}
测试结果:
至此,完成了spring和mybatis的整合。
关于整合spring,我的理解是,将一些控制权交给了spring,可能注意到了,这个项目没上传mybatis-config.xml,因为我的星目中这个文件整合spring之后就是空的了,所以没传。我将里面的配置文件全部加进了spring的配置文件applicationContext中:数据库的连接,sql session工厂,以及映射。
2.进行整合spring之后的配置文件的理解
之后进行了一些配置文件的理解:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 采用自动扫描方式创建mapper bean-->
<property name="basePackage" value="com.student.mapper"/><!--basePackage:扫描器开始扫描的基础包名。也就是后面的包是开始扫描的基础包-->
主要就是上面这个,其他的都在之前的mybatis里面接触过。
3.然后整合ssm,比预料中要轻松一些:
不过整合之后的项目出现了一些问题,就是修改和增加学生信息的时候,点击提交之后出现问题:问题如下:
上面这个是更新信息提交之后的界面;下面是增加信息提交之后的界面:
第一此碰到这样的情况,询问师兄一波,师兄建议我试试普通的表单提交,说spring的表单提交总是出现一些神奇的问题,打算明天解决。
接下来上传一下ssm搭建完之后的目录:
上面的就是完整的工程目录了。之前已经完成了spring+jdbc+springmvc的一个项目,两者对比起来目前没发现太大的不同,因为知识按照教程搭建了ssm,配置文件还没有进行理解,打算明天将bug修改完毕之后进行配置文件的理解。
4.搭建了半天,都还不知道ssm到底是干啥的,于是找了些文档看了看:
(1)ssm的工作流程:
客户端发送请求,后台服务器接受到请求首先被控制层(Controller)拦截,根据映射关系调用业务层(Service)的方法,找到逻辑层(ServiceImpl) ,然后通过持久层(Mapper / Dao)获取对象,对数据库进行操作,查询到的结果存储在实体类(Entity)中,最后这个结果数据会被一层一层的返回到客户端。
今天的收获
今天完成了spring和mybatis的整合,并且对整个过程比较清楚,然后算是初步完成了ssm的搭建。
今天遇到的问题
(1)ssm配置文件不是很懂
(2)ssm搭建之后存在bug
明天计划的事情
将今天的bug修改好;然后理解ssm配置文件的含义;再好好沉淀一下,梳理一下知识点。当然这一切要建立在顺利的基础上。
最后,麻烦师兄审核。
评论