Spring整合myBaties -- xml形式
作者:互联网
前言
刚接触到spring就被spring整合myBaties搞蒙了,在此整理一个顺序,以备后面需要.
第一步 在pom.xml中导入需要的坐标
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.12</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.8</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.16</version>
</dependency>
<!--slf4j日志-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<!--mysql连接驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<!--mybatis核心坐标-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<!--mybatis提供的与spring整合的坐标-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.1</version>
</dependency>
<!--tx-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--spring整合junit坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
<!--pageHelper分页坐标-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
第二步 创建实体类
import lombok.Data;
@Data
public class User {
private String username;
private String password;
}
第三步 创建mapper类和service类
mapper
这边为了方便直接写了注解
import com.guoliu.pojo.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface UserMapper {
@Select("select * from `user`")
List<User> findAll();
}
service
import com.guoliu.pojo.User;
import java.util.List;
public interface UserService {
List<User> findAll();
}
service实现类
import com.guoliu.mapper.UserMapper;
import com.guoliu.pojo.User;
import com.guoliu.service.UserService;
import java.util.List;
public class UserServiceImpl implements UserService {
private UserMapper userMapper;
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
public List<User> findAll() {
return userMapper.findAll();
}
}
第四步 编写配置文件
druid.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/db9
jdbc.username=root
jdbc.password=guoliu
spring.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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--1.导入资源配置文件-->
<context:property-placeholder location="classpath:druid.properties"/>
<!--2.配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--3.将mybaties交给spring管理-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--3.1注入数据源-->
<property name="dataSource" ref="dataSource"/>
<!--3.2注入映射配置文件位置-->
<property name="typeAliasesPackage" value="com.guoliu.pojo"/>
<!--分页插件-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<!--指定方言:指定数据源底层默认使用的数据库-->
<prop key="dialectHelper">mysql</prop>
<prop key="reasonable">true</prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
<!--4.mapper代理对象:扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--4.1注入sqlSessionFactory对象-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--4.2basePackage:指定需要生成代理对象的接口包-->
<property name="basePackage" value="com.guoliu.mapper"/>
</bean>
<!--5.service-->
<bean id="userService" class="com.guoliu.service.impl.UserServiceImpl">
<!--注入mapper对象-->
<property name="userMapper" ref="userMapper"/>
</bean>
</beans>
第五步 测试
import com.guoliu.pojo.User;
import com.guoliu.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class controller {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
UserService service = ac.getBean(UserService.class);
List<User> list = service.findAll();
System.out.println(list);
}
}
标签:myBaties,xml,--,spring,guoliu,import,org,com,public 来源: https://blog.csdn.net/weixin_47303159/article/details/121588576