文章目录
一、整合MyBatis
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--加载sql映射文件-->
<mappers>
<package name="com.jkj.Mapper"/>
</mappers>
</configuration>
1.将SqlSessionFactory相关的配置转化为:
@Bean
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setTypeAliasesPackage("com.jkj.pojo");
factory.setDataSource(dataSource);
return factory;
}
2.映射配置转化为:
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("com.jkj.dao");
return msc;
}
3.案例
首先创建一个数据库Spring设计一个表,表名为j_account 1.j_account 2.导入依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
3.Account
package com.jkj.pojo;
public class Account {
private Integer id;
private String name;
private Double money;
public Account() {
}
public Account(Integer id, String name, Double money) {
this.id = id;
this.name = name;
this.money = money;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
4.AccountDao
package com.jkj.dao;
import com.jkj.pojo.Account;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface AccountDao {
@Select("select * from j_account")
List<Account> findAll();
@Select("select * from j_account where id=#{id}")
Account FindById(Integer id);
@Insert("insert into j_account (id,name,money) values(id#{id},#{name},#{money})")
void save(Account account);
@Delete("delete from j_account where id=#{id}")
void delete(Integer id);
@Update("update j_account set name=#{name},money=#{money} where id=#{id}")
Account update(Integer id);
}
5.AccountService
package com.jkj.service;
import com.jkj.pojo.Account;
import java.util.List;
public interface AccountService {
public List<Account> findAll();
public Account FindById(Integer id);
public void save(Account account);
public void delete(Integer id);
public Account update(Integer id);
}
6.AccountServiceImpl
package com.jkj.service.impl;
import com.jkj.dao.AccountDao;
import com.jkj.pojo.Account;
import com.jkj.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
public List<Account> findAll() {
return accountDao.findAll();
}
public Account FindById(Integer id) {
return accountDao.FindById(id);
}
public void save(Account account) {
accountDao.save(account);
}
public void delete(Integer id) {
accountDao.delete(id);
}
public Account update(Integer id) {
return accountDao.update(id);
}
}
7.JdbcConfig
package com.jkj.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
public class JdbcConfig {
@Value("com.mysql.jdbc.Driver")
private String driver;
@Value("jdbc:mysql://localhost:3306/spring")
private String url;
@Value("root")
private String username;
@Value("root")
private String password;
@Bean
public DataSource dataSource(){
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
}
8.MyBatisConfig
package com.jkj.config;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
public class MyBatisConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setTypeAliasesPackage("com.jkj.pojo");
factory.setDataSource(dataSource);
return factory;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("com.jkj.dao");
return msc;
}
}
9.SpringConfig
package com.jkj.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ComponentScan("com.jkj")
@PropertySource("jdbc.properties")
@Import({JdbcConfig.class,MyBatisConfig.class})
public class SpringConfig {
}
10.Test测试类
import com.jkj.config.SpringConfig;
import com.jkj.pojo.Account;
import com.jkj.service.AccountService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
AccountService bean = context.getBean(AccountService.class,true);
List<Account> all = bean.findAll();
for (Account account : all) {
System.out.println(account);
}
}
}
二、整合Junit
1.导入依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
2.使用Spring整合Junit专用类加载器
package com.jkj;
import com.jkj.config.SpringConfig;
import com.jkj.pojo.Account;
import com.jkj.service.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class ServiceTest {
@Autowired
private AccountService as;
@Test
public void test(){
Account byId = as.FindById(1);
System.out.println(byId);
}
}
|