分类:JavaSE技术原创文章
作者:屈三岁
第一步:添加坐标
<dependencies>
<!--spring坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.1</version>
</dependency>
<!--数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
<!--springJDBC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<!-- druid数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
</dependencies>
第二步:写配置
<?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">
<!--配置数据源-->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/oneschool?serverTimezone=GMT"></property>
<property name="username" value="XXX"></property>
<property name="password" value="XXX" ></property>
</bean>
<!-- 加载工厂类-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="factory" >
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--将mapper映射器生成代理对象并注入spring容器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="configurer">
<property name="basePackage" value="com.querpu.dao"></property>
</bean>
<!-- 扫描包-->
<context:component-scan base-package="com.querpu"></context:component-scan>
</beans>
第三步:写dao层
package com.querpu.dao;
import com.querpu.pojo.Student;
import org.apache.ibatis.annotations.Select;
import org.springframework.context.annotation.Configuration;
import java.util.List;
/**
* @Description:
* @ClassName: IStudentDAO
* @Author: 屈小鲜肉
* @Date: 2021/3/24 9:26
* @Version: 1.0
*/
@Repository
public interface IStudentDAO {
@Select("select * from student")
public List<Student> selectStudent();
}
第四步:写service层
package com.querpu.service;
import com.querpu.dao.IStudentDAO;
import com.querpu.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Description:
* @ClassName: StudentServiceImp
* @Author: 屈小鲜肉
* @Date: 2021/3/24 9:31
* @Version: 1.0
*/
@Service
public class StudentServiceImp implements IStudentService{
@Autowired
private IStudentDAO studentDAO;
public List<Student> select() {
return studentDAO.selectStudent();
}
}
测试:
package com.querpu.test;
import com.querpu.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
/**
* @Description:
* @ClassName: Test01
* @Author: 屈小鲜肉
* @Date: 2021/3/24 9:40
* @Version: 1.0
*/
public class Test01 {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("appliactionContext.xml");
IStudentService bean = (IStudentService) applicationContext.getBean("studentServiceImp");
System.out.println(bean.select());
}
}