Spring 整合

1.Spring 整合 Junit

1.1新建项目结构

1.2导入依赖

导入 junit 与 Spring 依赖

org.springframework

spring-context

5.2.17.RELEASE

junit

junit

4.12

test

org.springframework

spring-test

5.2.7.RELEASE

1.3正常的写 spring 代码

添加 mapper @Repository

public class StudentMapperImpl implements StudentMapper {

public void save() {

System.out.println("保存操作");

}

}

添加配置类 @Configuration

@ComponentScan("cn.sycoder")

public class SpringConfig {

}

1.4通过测试类拿去mapper

先建立测试类 @RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(classes = {SpringConfig.class})

public class StudentTest {

@Autowired

private StudentMapper studentMapper;

@Test

public void testStudentMapper(){

studentMapper.save();

}

}

1.5@Runwith

说明@Runwith位置测试类上方作用测试类注解,设置 junit 运行器的属性使用测试运行的环境,SpringJUnit4ClassRunner.class

1.5@ContextConfiguration

说明@ContextConfiguration位置测试类上方作用设置 junit 加载 spring 配置类属性classes:核心配置类,可以传入多个locations:配置文件的文件路径名称

2.Spring整合JdbcTemplate

概述:jdbcTemplate 是 spring 对于学过的 jdbc 的封装,实现了方便操作数据库

2.1导入依赖

导入依赖

org.springframework

spring-orm

5.2.17.RELEASE

mysql

mysql-connector-java

8.0.29

com.alibaba

druid

1.2.8

org.projectlombok

lombok

1.18.22

2.2建立 jdbc.propreties

创建配置文件 jdbc.username=root

jdbc.password=123456

jdbc.driverClassName=com.mysql.cj.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/mybatis

2.3配置druid 连接池

配置类 @Configuration

@PropertySource("jdbc.properties")

public class DruidConfig {

@Value("${jdbc.username}")

private String username;

@Value("${jdbc.password}")

private String password;

@Value("${jdbc.url}")

private String url;

@Value("${jdbc.driverClassName}")

private String driver;

@Bean

public DataSource dataSource(){

final DruidDataSource source = new DruidDataSource();

source.setUsername(username);

source.setPassword(password);

source.setDriverClassName(driver);

source.setUrl(url);

return source;

}

}

配置 jdbcTemplate 类 @Configuration

@PropertySource("jdbc.properties")

public class DruidConfig {

@Value("${jdbc.username}")

private String username;

@Value("${jdbc.password}")

private String password;

@Value("${jdbc.url}")

private String url;

@Value("${jdbc.driverClassName}")

private String driver;

@Bean

public DataSource dataSource(){

final DruidDataSource source = new DruidDataSource();

source.setUsername(username);

source.setPassword(password);

source.setDriverClassName(driver);

source.setUrl(url);

return source;

}

@Bean

public JdbcTemplate jdbcTemplate(DataSource dataSource){

JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

return jdbcTemplate;

}

}

2.4修改 mapper 实现类做保存和查询操作

修改保存 @Repository

public class StudentMapperImpl implements StudentMapper {

@Autowired

private JdbcTemplate template;

public void save() {

String sql = "insert into student value(null,?,?)";

template.update(sql,"sy",18);

}

public Student getById(Long id) {

Object student = template.queryForObject("select * from student where id = ?",

new BeanPropertyRowMapper(Student.class), id);

return (Student) student;

// return null;

}

}

相关文章

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。