spring-mybatis整合

spring整合mybatis方式一个:使用spring自动注入Mapper接口实现类的对象:使用MapperScannerConfigurer
详细介绍:> https://blog.csdn.net/qq_40863603/article/details/86070139

pom.xml中的依赖:
`


junit
junit
4.13.2
test



mysql
mysql-connector-java
8.0.28



org.mybatis
mybatis
3.5.2


org.springframework
spring-aop
5.3.16



org.springframework
spring-jdbc
5.3.15



org.mybatis
mybatis-spring
2.0.2



org.aspectj
aspectjweaver
1.9.8



org.springframework
spring-context
5.3.16

`

编写一个外部配置文件:
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/studb jdbc.username=自己数据库的id jdbc.password=密码

编写一个pojo类:
public class User { private Integer id; private String username; private String password; 里面的set,get,tostirng,Constructor}
编写一个UserMapper接口:
@Repository public interface UserMapper { List<User> selectAll(); }
编写一个UserMapper.xml映射文件:
`

编写一个spring-mybatis.xml配置文件:

<context:component-scan base-package="com"/>


<context:property-placeholder location="db.properties"/>
























编写一个测试类: @Test
public void test01(){
ApplicationContext context=new ClassPathXmlApplicationContext("spring-mybatis.xml");
UserMapper userMapper = context.getBean(UserMapper.class);
List users =
userMapper.selectAll();
for (User user : users) {
System.out.println(user);
}

}`

https://blog.csdn.net/qq_41696858/article/details/98216237:里面介绍了MapperFactoryBean和MapperScannerConfiguration的作用

posted @ 2022-03-08 08:55  当个码农吧  阅读(52)  评论(0)    收藏  举报