一、定义一个类作为主配置这里命名为SpringConfig
操作:通过注解①@Configuration定义为主配置,同时导入前篇配置好的两个配置类通过@Import导入,③通过包扫描获取到service层和web层
@Configuration
//导入两个配置类
@Import({JdbcConfig.class,MybatisConfig.class})
//包扫描
@ComponentScan({"com.itheima"})
public class SpringConfig {
}
二、配置dao层(数据访问层)
正常配置即可,通常简单的还是配置成注解访问,除非特别麻烦的动态sql还是映射配置文件
public interface InfoMapper {
//查询所有数据
@Select("select * from tb_trip")
List<Trip> selectAll();
//添加用户数据
@Insert("insert into tb_trip values (null,#{username},#{gender},#{idcard},#{fromAddress},#{toAddress},#{startTime});")
void add(Trip trip);
//根据用户id查询数据
@Select("select * from tb_trip where id=#{id};")
Trip select(int id);
//根据id修改用户
void setInfo(int id);
//根据id删除用户
@Delete("delete from tb_trip where id=#{id} ;")
void delete(int id);
}
三、数据库字段对应的实体类这里忽略
四、service层(逻辑层)
先将从数据访问层那边的做成接口,通过实现接口的实体类进行操作
说明:这里要定义成接口是为了降低耦合性,有方法,但具体实现给实体类操作。不会因为实体类修改了,而修改过多数据
这里展示了一个方法:要加上@Service可以被包扫描到,@Autowired暴力映射,这里映射的是dao层
对@Service说明:这里本来是写@Component,@Service是派生出来的,可以见名知意。
指对三层web层:@Component写@Controller,
逻辑层:@Component写@Service,
dao层@Component写@Repository
@Service
public class InfoSerT implements InfoSer {
@Autowired
private InfoMapper infoMapper;
@Override
public List<Trip> selectALl() {
List<Trip> trips = infoMapper.selectAll();
return trips;
}