文章目录
前言
Mybatis-Spring整合:http://mybatis.org/spring/zh/index.html
一、导入依赖
pom.xml
<dependencies>
<!-- Spring核心 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.9</version>
</dependency>
<!--MySQL-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<!--MyBatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!--Spring整合MyBatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!--Spring整合JDBC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.9</version>
</dependency>
<!-- druid阿里的数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.20</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
二、编写User实体类
User
@Data
@Accessors(chain = true)
public class User implements Serializable {
private Integer id;
private String name;
private Integer age;
private String sex;
}
三、编写持久层UserMapper接口
UserMapper
public interface UserMapper {
List<User> getAllUser();
}
四、编写UserMapper映射文件
UserMapper.xml
<?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="cn.cy.mapper.UserMapper">
<select id="getAllUser" resultType="cn.cy.pojo.User">
select * from demo_user
</select>
</mapper>
五、编写jdbc.properties配置文件
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/demo_admin?useSSL=false&useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
六、编写springContext.xml配置文件
springContext.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
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 加载数据库连接信息的属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置Druid数据源的Bean -->
<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}"/>
<property name="initialSize" value="5"/>
<property name="maxActive" value="5"/>
</bean>
<!-- 配置SessionFactory的Bean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 指定MyBatis配置文件的位置 -->
<property name="mapperLocations" value="classpath:mappers/*.xml"/>
<!-- 给实体类起别名 -->
<property name="typeAliasesPackage" value="cn.cy.pojo"/>
</bean>
<!-- 配置mapper接口的扫描器,将Mapper接口的实现类自动注入到IoC容器中
实现类Bean的名称默认为接口类名的首字母小写 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!-- basePackage属性指定自动扫描mapper接口所在的包 -->
<property name="basePackage" value="cn.cy.mapper"/>
</bean>
</beans>
七、编写UserMapperTests测试类
UserMapperTests
public class UserMapperTests {
private ApplicationContext ac;
@Before
public void init() {
ac = new ClassPathXmlApplicationContext("springContext.xml");
}
@Test
public void getAllUser() {
UserMapper mapper = ac.getBean(UserMapper.class);
List<User> users = mapper.getAllUser();
// [User(id=1, name=zs, age=18, sex=男), User(id=2, name=ls, age=20, sex=男), User(id=3, name=xiaohong, age=18, sex=女)]
System.out.println(users);
}
}
八、demo_user数据库表模板
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/107652.html