15-spring整合mybatis方式一

src/main/java

**/*.properties

**/*.xml

true

代码演示

第一步 User类

package com.kuang.pojo;

import lombok.Data;

@Data

public class User {

private int id;

private String name;

private String pwd;

}

第二步 UserMapper接口

package com.kuang.mapper;

import com.kuang.pojo.User;

import java.util.List;

public interface UserMapper {

public List selectUser();

}

第三步 UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>

select * from mybatis.user;

第四步 核心配置文件

db.properties

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&characterEncoding=utf8

username=root

password=root

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>

spring-dao.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:aop=“http://www.springframework.org/schema/aop”

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">

applicationContext.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:aop=“http://www.springframework.org/schema/aop”

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">

第五步 UserMapperImpl类

package com.kuang.mapper;

import com.kuang.pojo.User;

import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

public class UserMapperImpl implements UserMapper{

//在原来,我们所有的操作,都使用sqlSession来执行,现在都使用SqlSessionTemplate

private SqlSessionTemplate sqlSession;

public void setSqlSession(SqlSessionTemplate sqlSession) {

this.sqlSession = sqlSession;

}

public List selectUser() {

UserMapper mapper= sqlSession.getMapper(UserMapper.class);

return mapper.selectUser();

}

}

第六步 测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值