发表于: 2020-06-15 22:49:57
1 1533
今天完成的事:完成spring和mybatis的整合
明天的计划:
1.把自己的项目部署到服务器上。
2.学习git
3.插入千万级数据进入数据库测试有索引和无索引的区别.
遇到的问题:无
收获:通过今天的学习,知道了spring和mybatis整合之后,spring专门管理bean,需要什么就注入什么,mybatis只负责用来编写sql语句操作数据库就行了。
今天的成果如下:
项目目录:
pom.xml配置文件(注意引入spring-mybatis整合包)
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>task1</artifactId>
<groupId>com.jnshu</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>SpringAndMybatis_crud</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.13.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.3</version>
</dependency>
</dependencies>
</project>
spring配置文件(设置 sqlSessionFactory 的 bean 实现类为 MyBatis 与 Spring 整合 jar 包中的 SqlSessionFactoryBean 类,在其中只需要注入两个参数:一个是 MyBatis 的全局配置文件,一个是上面配置的数据源 bean)
<?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
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 加载配置文件 --><context:property-placeholder location="classpath:db.properties"/>
<context:component-scan base-package="dao"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
<!-- sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载 MyBatis 的配置文件 -->
<property name="configLocation" value="MybatisConfig/mybatisConfig.xml"/>
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="studentDao" class="dao.StudentDaoImpl">
<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>
<package name="pojo"/>
</typeAliases>
<!-- 加载映射文件 -->
<mappers>
<!-- 通过 resource 一次加载一个映射文件 -->
<mapper resource="Mapper/studentMapper.xml"/>
</mappers>
</configuration>
用来操作数据库的studenMapper文件
<?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="student">
<!-- column:数据库的列名 property:要映射到实体类的字段名-->
<resultMap id="studentMap" type="student">
<!--要映射到实例类的主键ID-->
<id column="id" property="id"/>
<!--普通属性-->
<result column="student_name" property="name"/>
<result column="enter_time" property="enterTime"/>
<result column="student_number" property="studentNumber"/>
<result column="qq" property="qqNumber"/>
<result column="school" property="school"/>
<result column="study_type" property="type"/>
<result column="log_link" property="logLink"/>
<result column="slogan" property="slogan"/>
<result column="brother" property="brother"/>
</resultMap>
<!--查询所有数据-->
<select id="selectAll" resultMap="studentMap">
select *from student_task1
</select>
<!--根据id查询-->
<select id="selectById" parameterType="java.lang.Integer" resultMap="studentMap">
select *from student_task1 where id=#{id}
</select>
<!--插入学生数据-->
<insert id="insertStudent" parameterType="student">
insert into student_task1(
student_name,enter_time,qq,school,student_number,study_type,log_link,slogan,brother,create_at,update_at)
values (
#{name},#{enterTime},#{qqNumber},#{school},#{studentNumber},#{type},#{logLink},#{slogan},#{brother},#{createTime},#{updateTime})
</insert>
<!--根据姓名模糊查询-->
<select id="selectByName" parameterType="java.lang.String" resultMap="studentMap">
select *from student_task1 where student_name like CONCAT(#{name},'%')
</select>
<!--根据学号模糊查询-->
<select id="selectByNumber" parameterType="java.lang.String" resultMap="studentMap">
select *from student_task1 where student_number like CONCAT(#{studentNumber},'%');
</select>
<!--根据id更新多种数据-->
<update id="updateById" parameterType="student">
update student_task1
<set>
<if test="studentNumber!=null">student_number=#{studentNumber},</if>
<if test="school!=null">school=#{school},</if>
<if test="qqNumber!=null">qq=#{qqNumber},</if>
<if test="type!=null">study_type=#{type},</if>
<if test="logLink!=null">log_link=#{logLink},</if>
<if test="slogan!=null">slogan=#{slogan},</if>
<if test="brother!=null">brother=#{brother},</if>
<if test="enterTime!=null">enter_time=#{enterTime},</if>
<if test="createTime!=null">create_at=#{createTime},</if>
<if test="name!=null">student_name=#{name}</if>
</set>
where id=#{id}
<!--根据id更新数据-->
</update>
<!--根据id删除-->
<delete id="deleteById" parameterType="java.lang.Integer">
delete from student_task1 where id=#{id}
</delete>
</mapper>
接口的具体实现类(
- StudentDaoImpl 不仅实现了 StudenDao 接口,而且继承了 SqlSessionDaoSupport 类。
- SqlSessionDaoSupport 类是 MyBatis 与 Spring 整合的 jar 包中提供的,在该类中已经包含了 sqlSessionFactory 对象作为其成员变量,而且对外提供 get 和 set 方法,方便 Spring 从外部注入 sqlSessionFactory 对象。
)
package dao;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;
import pojo.Student;
import java.util.List;
/**
* @ClassName StudentDaoImpl
* @Description 接口实现类
* @Author 韦延伦
* @Date 2020/6/15 17:16
* @Version 1.0
*/
@Repository("studentDao")
public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentDao {
public int insert(Student student) {
SqlSession sqlSession = this.getSqlSession();
sqlSession.insert("insertStudent", student);
return 0;
}
public boolean deleteById(int id) {
SqlSession sqlSession = this.getSqlSession();
sqlSession.delete("deleteById", id);
return false;
}
public boolean updateById(Student student) {
SqlSession sqlSession = this.getSqlSession();
sqlSession.update("updateById", student);
return false;
}
public List<Student> selectAll() {
SqlSession sqlSession = this.getSqlSession();
return sqlSession.selectList("selectAll");
}
public List<Student> selectById(int id) {
SqlSession sqlSession = this.getSqlSession();
return sqlSession.selectList("selectById", id);
}
public List<Student> selectByNumber(String number) {
SqlSession sqlSession = this.getSqlSession();
return sqlSession.selectList("selectByNumber", number);
}
public List<Student> selectByName(String name) {
return this.getSqlSession().selectList("selectByName", name);
}
}
测试类
import dao.StudentDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import pojo.Student;
import java.util.List;
/**
* @ClassName Test
* @Description 测试类
* @Author 韦延伦
* @Date 2020/6/15 17:47
* @Version 1.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:Spring/Application.xml")
public class TestSpringAndMybatis {
//自动注入studentdao
@Autowired
private StudentDao studentDao;
/**
* @return void
* @Author 韦延伦
* @Description 测试插入数据
* @Date 2020/6/15 18:28
* @Param []
**/
@Test
public void testInsert() {
Student student = new Student();
student.setName("韦延伦");
student.setEnterTime("20200605");
student.setQqNumber("9392");
student.setType("java");
student.setStudentNumber("6603");
student.setSlogan("加油思密达 ");
student.setLogLink("www.jnshu.com");
student.setSchool("修真院");
student.setUpdateTime(System.currentTimeMillis());
student.setCreateTime(System.currentTimeMillis());
student.setBrother("invild s");
studentDao.insert(student);
}
/**
* @return void
* @Author 韦延伦
* @Description 测试更新数据
* @Date 2020/6/15 18:28
* @Param []
**/
@Test
public void testUpdateById() {
Student student = new Student();
student.setName("到我了没有");
student.setId(1);
studentDao.updateById(student);
}
/**
* @return void
* @Author 韦延伦
* @Description 测试根据id进行删除
* @Date 2020/6/15 18:53
* @Param []
**/
@Test
public void testDeleteById() {
studentDao.deleteById(1);
}
/**
* @return void
* @Author 韦延伦
* @Description 测试查询所有数据
* @Date 2020/6/15 18:50
* @Param []
**/
@Test
public void testSelectAll() {
List<Student> studentList = studentDao.selectAll();
for (Student student : studentList) {
outPutStudent(student);
System.out.println("------分割线------");
}
}
/**
* @return void
* @Author 韦延伦
* @Description 测试按照id查询
* @Date 2020/6/15 18:49
* @Param []
**/
@Test
public void testSelectById() {
List<Student> studentList = studentDao.selectById(2);
for (Student student : studentList) {
outPutStudent(student);
System.out.println("------分割线------");
}
}
/**
* @return void
* @Author 韦延伦
* @Description 测试按照姓名查询
* @Date 2020/6/15 18:49
* @Param []
**/
@Test
public void testSelectByName() {
List<Student> studentList = studentDao.selectByName("韦延伦");
for (Student student : studentList) {
outPutStudent(student);
System.out.println("------分割线------");
}
}
/**
* @return void
* @Author 韦延伦
* @Description 测试按照学号查询
* @Date 2020/6/15 18:49
* @Param []
**/
@Test
public void testSelectByNumber() {
List<Student> studentList = studentDao.selectByNumber("6603");
for (Student student : studentList) {
outPutStudent(student);
System.out.println("------分割线------");
}
}
/**
* @return void
* @Author 韦延伦
* @Description 输出学生信息的方法
* @Date 2020/6/15 18:49
* @Param [student]
**/
private void outPutStudent(Student student) {
System.out.println("姓名:\t" + student.getName());
System.out.println("入学时间:\t" + student.getEnterTime());
System.out.println("学号:\t" + student.getStudentNumber());
System.out.println("QQ:\t " + student.getQqNumber());
System.out.println("学校:\t" + student.getSchool());
System.out.println("修真类型:\t" + student.getType());
System.out.println("日报链接:\t" + student.getLogLink());
System.out.println("立愿:\t" + student.getSlogan());
System.out.println("辅导师兄:\t" + student.getBrother());
}
}
评论