mybatis整合spring 一起使用
1、配置pom.xml文件,配置mybatis、spring依赖 以及阿里公司的数据库连接池
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!--做spring事务-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<!--mybatis 和springj集成-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<!--阿里公司的数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<build>
<!--把src-main-java目录中xml文件包含到输出结果中,输出到classes目录中-->
<resources>
<resource>
<directory>src/main/java</directory> <!--所在的目录-->
<includes> <!--包括目录下的.properties,.xml文件都会扫描到-->
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>13</source>
<target>13</target>
</configuration>
</plugin>
</plugins>
</build>
2、新建daomain目录且创建Student类,属性要和数据库中表的数据对应,实现set()、get()、toString()方法即可。
3、新建dao目录且创建StudentDao接口,并且在接口中创建CRUD方法。
int insertStudent(Student student);
int updateStudent(Student student);
int deleteStudent(Student student);
List<Student> selectStudents();
4、在StudentDao同目录下创建StudentDao.xml文件,即用来配置CRUD的具体操作。
<?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.octheng.dao.StudentDao">
<insert id="insertStudent">
insert into student values(#{id},#{name},#{email},#{age})
</insert>
<update id="updateStudent">
update student set id=#{id},name=#{name},email=#{email},age=#{age} where name="huangzhong"
</update>
<delete id="deleteStudent">
delete from student where id=#{id}
</delete>
<select id="selectStudents" resultType="com.octheng.daomain.Student">
select id,name,email,age from student order by id desc;
</select>
</mapper>
5、在dao包的同目录下创建service目录,同时创建StudentService接口,创建CRUD操作,尽量不要和上面的StudentDao中的CRUD重名。
int insertService(Student student);
int updateService(Student student);
int deleteService(Student student);
List<Student> selectService();
同时在该包下新建包并且实现StudentService接口。
private StudentDao studentDao;
public StudentDao getStudentDao() {
return studentDao;
}
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public int insertService(Student student) {
int nums = studentDao.insertStudent(student);
return nums;
}
@Override
public int updateService(Student student) {
int nums = studentDao.updateStudent(student);
return nums;
}
@Override
public int deleteService(Student student) {
int nums = studentDao.deleteStudent(student);
return nums;
}
@Override
public List<Student> selectService() {
List<Student> students = studentDao.selectStudents();
return students;
}
6、resources目录下编写mybatis.xml配置文件。这个配置文件主要是实现对包daomain中的类加载以及dao中所有xml文件的加载。
<?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>
<!--添加日志信息-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<package name="com.octheng.daomain"/>
</typeAliases>
<mappers>
<package name="com.octheng.dao"/>
<!-- 以上配置就是添加了这个包下所有的xml文件,下面配置只是加载一个
<mapper resource="dao/StudentDao.xml"/> -->
</mappers>
</configuration>
7、配置applicationContext.xml文件。这个文件中进行数据库的连接,并且通过扫描dao中所有接口得到对象,一般以接口的首字母小写命名。最后一个是创建StudentService接口的对象studentService,并且是把上面创建出来存在spring容器中studentDao对象通过引用赋值给StudentServiceImpl中的属性studentDao。
<?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:util="http://www.springframework.org/schema/util"
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/util https://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:mysql.properties"/>
<!--声明数据源,链接数据库-->
<bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!--set 注入给DruidDataSource提供链接数据库信息-->
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
<property name="maxActive" value="${mysql.maxActive}"/>
</bean>
<!--声明的是mybatis中提供的SqlSessionFactoryBean类,这个类内部创建SqlSessionFactory的-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--set注入,把数据库连接池付给dataSource属性-->
<property name="dataSource" ref="myDataSource"/>
<!--mybatis 主配置文件位置
configlocation属性是Resource类型,读取配置文件
赋值用value,指定文件的路径,使用classpqth:表示文件的位置-->
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
<!--创建dao对象,使用SqlSession的getMapper(StudentDao.class)
MapperScannerConfigurer:在内部调用getMapper()生成每个dao接口的代理对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定SqlSessionFactory对象的id-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--指定包名,包名是dao接口所在的包名。MapperScannerConfigurer会扫描这个包中所有接口
把每个接口都执行一遍getMapper()方法,得到每个接口的dao对象,创建好的dao对象放入spring容器中
这里就会得到studentDao对象,可以直接用来使用。-->
<property name="basePackage" value="com.octheng.dao"/>
</bean>
<bean id="studentService" class="com.octheng.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao"/>
</bean>
</beans>
这样spring容器中就有了两个对象,一个是spring内部执行getMapper()方法时得到的studentDao对象,另一个就是StudentService接口的studentService对象。在测试中就可以通过getBean()方法得到这两个对象直接拿来用。
public void test02() {
String config = "applicationContext.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
StudentDao dao = (StudentDao) ctx.getBean("studentDao");
//StudentService service = (StudentService) ctx.getBean("studentService");
Student student = new Student();
student.setId(10011);
student.setName("xiaozhang");
student.setEmail("15555555331@qq.com");
student.setAge(15);
int nums = dao.insertStudent(student);
// int nums = service.insertService(student);
System.out.println("nums" + nums);