spring-mybatis整合
spring整合mybatis方式一个:使用spring自动注入Mapper接口实现类的对象:使用MapperScannerConfigurer
详细介绍:> https://blog.csdn.net/qq_40863603/article/details/86070139
pom.xml中的依赖:
`
`
编写一个外部配置文件:
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
userMapper.selectAll();
for (User user : users) {
System.out.println(user);
}
}`
https://blog.csdn.net/qq_41696858/article/details/98216237:里面介绍了MapperFactoryBean和MapperScannerConfiguration的作用

浙公网安备 33010602011771号