文章目录
前言
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>
<!--Spring-test测试-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.9</version>
<scope>test</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
@Mapper
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
六、编写mybatisConfig.xml配置文件
这个配置文件可写可不写,就是将一些参数配置到配置文件,然后通过Spring框架引入到Java代码中。也可以直接写到Java代码中。
mybatisConfig.xml
Resource=classpath:mappers/*.xml
TypeAliasesPackage=cn.cy.pojo
七、编写AppConfig配置类
AppConfig
@ComponentScan(basePackages = "cn.cy")
@Configuration
@PropertySource(value = {"classpath:jdbc.properties","mybatisConfig.properties"})
@MapperScan("cn.cy.mapper")
public class AppConfig {
@Value("${jdbc.driver}")
private String driverClass;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Value("${Resource}")
private String mapperLocations;
@Value("${TypeAliasesPackage}")
private String TypeAliasesPackage;
//创建数据源对象,并将对象交给IOC容器管理
@Bean
public DataSource dastaSource() {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driverClass);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
return druidDataSource;
}
// 创建SqlSessionFactoryBean对象,设置数据源,mapperXML映射,设置别名,并将对象交给IOC容器管理
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) throws IOException {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources(mapperLocations);
sqlSessionFactoryBean.setMapperLocations(resources);
sqlSessionFactoryBean.setTypeAliasesPackage(TypeAliasesPackage);
return sqlSessionFactoryBean;
}
}
八、编写UserMapperTests测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class)
public class UserMapperTests {
@Autowired
UserMapper userMapper;
@Test
public void getAllUser() {
List<User> users = userMapper.getAllUser();
System.out.println(users);
}
}
九、demo_user数据库表模板
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/107651.html