mybatis和spring 两框架相结合-代码的具体实现
1.总项目结构
2.pojo包
3.mapper包
mapper.xml
4.service包
实现类
package com.wj.service.impl;
import com.wj.domain.Student;
import com.wj.mapper.IStudentMapper;
import com.wj.service.IStudentService;
import java.util.List;
/**
* 描述:
*
* @Author: 枭雄
* @Create By: 2021/3/10 19:17
*/
public class StudentServiceImpl implements IStudentService {
private IStudentMapper iStudentMapper;
@Override
public List<Student> findAll() {
return iStudentMapper.findAll();
}
@Override
public int save(Student student) {
return iStudentMapper.save(student);
}
@Override
public int update(Student student) {
return iStudentMapper.update(student);
}
@Override
public int delete(Integer id) {
return iStudentMapper.delete(id);
}
@Override
public Student findById(Integer id) {
return iStudentMapper.findById(id);
}
public IStudentMapper getiStudentMapper() {
return iStudentMapper;
}
public void setiStudentMapper(IStudentMapper iStudentMapper) {
this.iStudentMapper = iStudentMapper;
}
}
5.resources参数配置包
5.1beans.xml
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd"
>
<!-- 配置数据源-->
<!--1.加载db.properties,通过文件加载器-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--2.配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" >
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置mybatis的SqlSessionFactoyr工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源-->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置别名-->
<property name="typeAliasesPackage" value="com.wj.domain"/>
</bean>
<!--配置动态代理DAO-->
<bean id="mapperScannerConfiger" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 批量注册-->
<property name="basePackage" value="com.wj.mapper"/>
</bean>
<!-- 配置业务层-->
<bean id="studentService" class="com.wj.service.impl.StudentServiceImpl">
<property name="iStudentMapper" ref="IStudentMapper"/>
</bean>
</beans>
5.2db.properties配置
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1/stu?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
jdbc.username=root
jdbc.password=root
5.3mybatis-config。xml 不需要内容,单配置文件需要存在
6.pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring_mybatis_together</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.23.RELEASE</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>