做网站费用多少钱,网站排名易下拉霸屏,百度给公司做网站效果咋样,展示类网站管理员1.纯注解开发【定义配置类的注解】
Confituration 表示该类是一个配置类 ComponentScan(“com.itheima”) 配置包扫描 PropertySource(“classpath:jdbc.properties”) 加载属性文件 Import(JdbcConfig.class) 加载其他配置类
2.spring整合mybatis【纯注解#xff0c;3个…1.纯注解开发【定义配置类的注解】
Confituration 表示该类是一个配置类 ComponentScan(“com.itheima”) 配置包扫描 PropertySource(“classpath:jdbc.properties”) 加载属性文件 Import(JdbcConfig.class) 加载其他配置类
2.spring整合mybatis【纯注解3个配置类】
1SpringConfig配置类
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 //表示该类是一个配置类用来代替applicationContext.xml这个注解可以不写但是一般都写了
//context:component-scan base-packagecom.itheima/
ComponentScan(com.itheima) //Spring包扫描
//context:property-placeholder locationclasspath*:*.properties/
PropertySource(classpath:jdbc.properties) //加载属性文件
// import resourceclasspath:applicationContext-dao.xml/
Import(JdbcConfig.class) //引入分配置类
public class SpringConfig {
}
2JdbcConfig配置类
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(${jdbc.driver})private String driverClassName;Value(${jdbc.url})private String url;Value(${jdbc.username})private String username;Value(${jdbc.password})private String password;Beanpublic DataSource dataSource() {DruidDataSource ds new DruidDataSource();ds.setDriverClassName(driverClassName);ds.setUrl(url);ds.setUsername(username);ds.setPassword(password);return ds;}
}
3MybatisConfig 配置
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;public class MybatisConfig {Beanpublic SqlSessionFactoryBean SqlSessionFactoryBean(Autowired DataSource dataSource){SqlSessionFactoryBean ssfb new SqlSessionFactoryBean();//1.注入连接池对象【必须】ssfb.setDataSource(dataSource);//2.加载MybatisConfig.xml核心配置文件// 如果核心配置文件中的内容都被抽取了那么就可以不用加载【可选】//ClassPathResource resource new ClassPathResource(classpath:MybatisConfig.xml);//ssfb.setConfigLocation(resource);//3 配置别名,如果是使用注解配置SQL语句可以不用配置别名【可选】//ssfb.setTypeAliasesPackage(com.itheima.bean);//4 加载映射配置文件如果映射配置文件和mapper接口在同一个包下并且同名那么会自动加载【可选】//ClassPathResource resource new ClassPathResource(classpath:StudentMapper.xml);//ssfb.setMapperLocations(resource);return ssfb;}Beanpublic MapperScannerConfigurer mapperScannerConfigurer(){MapperScannerConfigurer msc new MapperScannerConfigurer();msc.setBasePackage(com.itheima.mapper);return msc;}
}
3.具体实现类及测试类
1StudentServiceImpl类
package com.itheima.service.impl;import com.itheima.bean.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.io.IOException;
import java.util.List;Service(studentService)
public class StudentServiceImpl implements StudentService {Autowiredprivate StudentMapper mapper;//注入StudentMapper的代理对象Overridepublic ListStudent selectAll() throws IOException {return mapper.selectAll();}Overridepublic Student selectById(Integer id) throws IOException {return mapper.selectById(id);}Overridepublic Integer insert(Student stu) throws IOException {return mapper.insert(stu);}Overridepublic Integer update(Student stu) throws IOException {return mapper.update(stu);}Overridepublic Integer delete(Integer id) throws IOException {return mapper.delete(id);}
}
2studentMapper类
package com.itheima.mapper;
import com.itheima.bean.Student;
import org.apache.ibatis.annotations.*;import java.util.List;
public interface StudentMapper {//查询全部Select(select * from student)public abstract ListStudent selectAll();//根据id查询Select(select * from student where id#{ID})public abstract Student selectById(Integer id);//新增数据Insert(insert into student values(null,#{name},#{age}))public abstract Integer insert(Student stu);//修改数据Update(update student set name#{name},age#{age} where id#{id})public abstract Integer update(Student stu);//删除数据Delete( delete from student where id#{id})public abstract Integer delete(Integer id);
}
3jdbc.properties文件
jdbc.drivercom.mysql.jdbc.Driver
jdbc.urljdbc:mysql://localhost:3306/mybatis
#不加Jdbc系统会默认username为系统用户名
jdbc.usernameroot
jdbc.password123456
❤️-4测试类
获取容器方式为加载配置类 Testpublic void test1() throws IOException {//从美IOC中取studentService对象//ApplicationContext applicationContext new ClassPathXmlApplicationContext(applicationContext.xml);ApplicationContext applicationContext new AnnotationConfigApplicationContext(SpringConfig.class);StudentService studentService applicationContext.getBean(studentService, StudentService.class);ListStudent list studentService.selectAll();list.forEach(stu- System.out.println(stu));}打印结果
---------------------------------------------------------------------
Student{id1, name张三, age23}
Student{id2, name李四, age24}
Student{id15, name小郭, age22}4.spring整合junit单元测试
前提添加spring-test和junit依赖并且junit依赖的版本必须大于等于4.12版本。
【第一步】 !--junit--dependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.12/version/dependency!--Spring整合junit单元测试--!--junit的依赖至少要是4.12版本,可以是4.13等版本,否则出现如下异常--dependencygroupIdorg.springframework/groupIdartifactIdspring-test/artifactIdversion5.1.9.RELEASE/version/dependency【第二步】 spring的Junit测试类整合
import com.itheima.bean.Student;
import com.itheima.config.SpringConfig;
import com.itheima.service.StudentService;
import org.junit.Assert;
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;import java.io.IOException;
import java.util.List;//【第二步】使用spring中提供的单元测试运行类替换JVM中单元测试运行类
RunWith(SpringJUnit4ClassRunner.class)
//【第三步】加载配置文件或者配置类[将bean对象注入容器]
ContextConfiguration(classes {SpringConfig.class})
public class StudentJunit {Autowired//[将studentService采用set注入方式则不能new容器对象获取]private StudentService studentService;Testpublic void test1() throws IOException {ListStudent list studentService.selectAll();//断言测试与预期一致则通过反正爆出不同的地方Assert.assertEquals(24,list.size());}Testpublic void test2() throws IOException {Student stu studentService.selectById(20);Assert.assertEquals(陈锦涛,stu.getName());}
}