发表于: 2020-10-11 22:59:30
1 1377
今天完成的事情:
整合Spring+Mybatis
创建数据表并导入数据
目录结构
导入applicationContext之前需要导入spring-context包
<!-- 导入spring-context包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
导入依赖包
<dependencies>
<!-- 导入mybatis包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- 导入mybatis-spring包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
<!-- 导入spring-core包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!-- 导入mysql-connector-java包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
</dependency>
<!-- 导入junit包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- 导入spring-context包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!-- 导入spring-jdbc包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
</dependencies>
创建db.properties连接数据库信息
#mysql
driver=com.mysql.jdbc.Driver
url=jdbc:mysql:///db4
user=root
password=wsj199599
创建student实体类
package com.jnshu.Pojo;
import java.sql.Date;
public class Student {
private int id;
private String name;
private int gender;
private Date birthday;
public Student(int id,String name,int gender,Date birthday){
this.id = id;
this.name = name;
this.gender = gender;
this.birthday = birthday;
}
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 getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
/**
* @Override 的作用是:如果想重写父类的方法,比如toString()方法的话,
* 在方法前面加上@Override 系统可以帮你检查方法的正确性。
*
* toString 方法会返回一个“以文本方式表示”此对象的字符串。
* @return
*/
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", gender=" + gender +
", birthday=" + birthday +
'}';
}
}
这里了解setter和getter方法的作用,还了解了@Override的作用 和 toString的方法的作用
setter和getter方法主要是为了调用封装的数据,保证数据的安全性。只可以读不能改写。
顺便了解一下封装
封装
java是面向对象的语言,为了让类的内部数据不被随意的访问修改,我们会用访问修饰符对其被访问权限进行修饰。例如我们经常看见的实体类。里面的成员变量我们就是用private修饰符进行修饰,然后提供getter、setter方法,这样外部就不能直接访问该类的成员变量了,只能通过get、set方法访问。总的来说封装就是我们用权限修饰符private对其方法和成员变量进行修饰,不让外部随意访问。
Getter和Setter方法参考网址(https://blog.csdn.net/jason_lh1024/article/details/90900339)
创建StudentDao接口
package com.jnshu.Mapper;
import com.jnshu.Pojo.Student;
import java.util.List;
public interface StudentDao {
public int intsertStudent(Student student);
public int deleteStudent(String name);
public int updateStudent(Student student);
public Student findStudentById(int id);
public List<Student> findAllStudent();
}
写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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="db.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 数据库驱动,我这里使用的是Mysql数据库 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<!-- 数据库地址,这里也要注意一下编码,不然乱码可是很郁闷的哦! -->
<property name="url" value="jdbc:mysql:///db4"/>
<!-- 数据库的用户名和密码-->
<property name="username" value="root"/>
<property name="password" value="wsj199599"/>
</bean>
<!-- 指定数据源和配置文件路径 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 自动扫描mapping.xml文件 -->
<property name="configLocation" value="mybatis-config.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类,并将其定义为一个Spring Bean -->
<bean id="studentDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.jnshu.Mapper.StudentDao"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
编写Mybatis的配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- 声明在标签里面的信息是配置信息 -->
<configuration>
<!-- 声明在该标签里面的信息是一个个的别名 -->
<typeAliases>
<!-- 声明要使用别名的对象(全路径)用java注解的话可以使用@Alias注解声明 -->
<typeAlias type="com.jnshu.pojo.Student" alias="student"/>
</typeAliases>
<!-- 声明我们定义的一个个Mapper类,或者说是关联 -->
<mappers>
<!-- 声明Mapper的路径 -->
<mapper resource="StudentDao.xml"/>
</mappers>
</configuration>
编写Student的mapper映射文件
<?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对应一个dao -->
<mapper namespace="com.jnshu.Mapper.StudentDao">
<!--增-->
<insert id="insertStudent" parameterType="com.jnshu.Pojo.Student" >
INSERT INTO student VALUES (#{id},#{name},#{gender},#{birthday},)
</insert>
<!--删-->
<delete id="deleteStudent" parameterType="String" >
DELETE FROM student WHERE NAME =#{name}
</delete>
<!--改-->
<update id="updateStudent" parameterType="com.jnshu.Pojo.Student">
UPDATE student SET weight=#{name}, WHERE ID=#{id}
</update>
<resultMap id="Students" type="com.jnshu.pojo.Student">
<id property="id" column="id" javaType="java.lang.Long"/>
<result property="id" column="id" javaType="java.lang.String"/>
<result property="name" column="name" javaType="java.lang.Integer"/>
<result property="gender" column="gender" javaType="java.lang.Integer"/>
<result property="birthday" column="birthday" javaType="java.lang.String"/>
</resultMap>
<!--通过id查找-->
<select id="findStudentById" parameterType="int" resultMap="com.jnshu.Pojo.Student">
SELECT * from student where id=#{id}
</select>
<!-- 封装List集合-->
<select id="getAllStudent" resultMap="com.jnshu.Pojo.Student">
select * from student
</select>
</mapper>
创建实体类接口Student的实现类StudentImple
package com.jnshu.Pojo;
import com.jnshu.Mapper.StudentDao;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;
public class StudentDaoImple extends SqlSessionDaoSupport implements StudentDao {
public int intsertStudent(Student student) {
return this.getSqlSession().insert("test.insertStudent",student);
}
public int deleteStudent(String name) {
SqlSession sqlSession = this.getSqlSession();
return sqlSession.delete("test.deleteStudent",name);
}
public int updateStudent(Student student) {
return this.getSqlSession().update("test.updateStudent",student);
}
public Student findStudentById(int id) {
return this.getSqlSession().selectOne("test.findStudentById",id);
}
public List<Student> findAllStudent() {
return this.getSqlSession().selectList("test.findAll");
}
}
编写测试类(暂时只写了一个)
import com.jnshu.Mapper.StudentDao;
import com.jnshu.Pojo.Student;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class StudentDaoImpleTest {
private ApplicationContext context;
@Before
public void setUp() throws Exception {
context = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@Test
public void insertStudent() {
StudentDao studentDao = (StudentDao) context.getBean("student");
Student student = new Student(1, "49.9", 21, "1997-12-11");
studentDao.intsertStudent(student);
System.out.println(student);
}
}
出现报错
明天计划的事情:
了解mybatis,使用mybatis连接数据库
收获:对Spring+Mybatis,有了初步的认识。知道自己对封装,抽象方法,@Override,继承和接口,映射太多基础的概念不了解。基础知识太薄弱
评论