1.用maven导入所需要的依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
2.数据库信息配置文件
首先在resource目录下创建一个db.properties文件,里面写上数据库连接的属性
3.读取配置信息
这里注意DataSource是DruidDataSource的接口,按道理来说返回接口就行了,但是实际上返回接口会报错,这里干脆直接返回实现类
package com.yc.pro1.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
@Configuration
public class JdbcConfig {
@Value("${uname}")
private String username;
@Value("${password}")
private String password;
@Value("${url}")
private String url;
@Value("${driverClassName}")
private String driverClassName;
@Bean()
public DruidDataSource druidDataSource() {
DruidDataSource ds = new DruidDataSource();
ds.setUsername(username);
ds.setPassword(password);
ds.setUrl(url);
ds.setDriverClassName(driverClassName);
return ds;
}
}
4.创建MyBatis的配置类
@Configuration
public class MybatisConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DruidDataSource dataSource) {
SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
ssfb.setTypeAliasesPackage("com.yc.pro1.domain");
ssfb.setDataSource(dataSource);
return ssfb;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("com.yc.pro1.dao");
return msc;
}
}
5.创建主配置类
@Configuration
@ComponentScan("com.yc.pro1")
@PropertySource("classpath:db.properties")
public class SpringConfig {
}
5.在Dao层创建一个接口
其中@Update代表要执行更新操作,#{}里面的值代表传入的参数
@Repository
public interface AccountDao {
@Update("update tb_account set money=money+#{money} where id=#{id}")
void addMoney(int id, double money);
@Update("update tb_account set money=money-#{money} where id=#{id}")
void subMoney(int id, double money);
@Select("select * from tb_account where id=#{id}")
List<Account> show(int id);
@Select("select * from tb_account")
List<Account> showAll();
}
6.Service层中创建接口
@Service
@Transactional // 声明该类中的方法都应该在事务中执行
public interface AccountService {
void addMoney(int id, double money);
void subMoney(int id, double money);
List<Account> show(int id);
List<Account> showAll();
}
7.实现Service中的接口
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
@Override
public void addMoney(int id, double money) {
accountDao.addMoney(id, money);
}
@Override
public void subMoney(int id, double money) {
accountDao.subMoney(id, money);
}
@Override
public List<Account> show(int id) {
return accountDao.show(id);
// 可以根据需要在这里处理查询结果或者返回结果
}
@Override
public List<Account> showAll() {
return accountDao.showAll();
}
}
8.封装数据
要写上getset方法和构造方法
public class Account implements Serializable {
Integer id;
String name;
Double money;
public Account(Integer id, String name, Double money) {
this.id = id;
this.name = name;
this.money = money;
}
9.测试
@Configuration
@ComponentScan("com.yc.pro1")
public class App {
public static void main(String[] args) {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(App.class);
AccountService bean = ac.getBean(AccountService.class);
List<Account> accounts =bean.showAll();
System.out.println(accounts);
}
}
运行结果如下