1 新建一个maven工程
2 配置mybatis相关
? ? ?1? 添加mybatis依赖
<!-- MyBatis依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
2??新建mybatis-config.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>
</configuration>
3 添加Lombok、junit依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
?3 部署Spring
框架
? ? ? 1? 添加依赖
<!-- Spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.18</version>
</dependency>
? ?2? ??创建applicationContext.xml配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
</beans>
4?Spring整合MyBatis?
? ? ?1 添加整合依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
? ?2??.配置数据源(Druid)
? ? ? ??添加druid依赖
<!-- druid依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
? ? ??创建druid.properties文件
druid.driver=com.mysql.cj.jdbc.Driver
druid.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
druid.username=root
druid.password=root
## 连接池参数
druid.pool.init=1
druid.pool.minIdle=3
druid.pool.maxActive=20
druid.pool.maxWait=30000
?Spring配置文件中配置DruidDataSource
?
<!--加载druid.properties文件-->
<context:property-placeholder location="classpath:druid.properties"/>
<!--数据源DataSource的创建-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${druid.driver}"/>
<property name="url" value="${druid.url}"/>
<property name="username" value="${druid.username}"/>
<property name="password" value="${druid.password}"/>
<property name="initialSize" value="${druid.pool.init}"/>
<property name="minIdle" value="${druid.pool.minIdle}"/>
<property name="maxActive" value="${druid.pool.maxActive}"/>
<property name="maxWait" value="${druid.pool.maxWait}"/>
</bean>
创建SqlSessionFactory
<!--MyBatis的SqlSessionFactory对象的创建-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置数据源-->
<property name="dataSource" ref="dataSource"/>
<!--配置Mapper文件路径-->
<property name="mapperLocations" value="classpath:mappers/*Mapper.xml"/>
<!--配置需要定义别名的实体类的包-->
<property name="typeAliasesPackage" value="com.gyh.pojo"/>
<!--配置MyBatis的配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
创建Mapper
<!--加载dao包中的所有接口,通过sqlSessionFactory获取sqlSession对象,然后创建所有的dao接口并存储在Spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.gyh.dao"/>
</bean>
使用示例
public interface DepMapper {
List<Dep> selectByExample();
}
<?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.dao.DepMapper" >
<select id="selectByExample" resultType="com.pojo.Dep" >
select * from dep
</select>
</mapper>
public class test {
@Test
public void selectByExample() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//UserDao userDao = context.getBean("userDao", UserDao.class);
DepMapper depMapper = context.getBean("depMapper", DepMapper.class);
List<Dep> deps = depMapper.selectByExample();
System.out.println("deps===" + deps);
}
}
|