Mybatis:
package com.aaa.Dao;
import com.aaa.Entity.Goods;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import java.util.List;
public interface GoodDao {
@Select({"select * from goods"})
List<Goods> findAllGoods();
@Update({"update goods set name=#{name} where id=#{id}"})
int updateGoods(Object goods);
@Insert({"insert into goods(name,create_time) values(#{name},#{createTime})"})
int insertGoods(Object goods);
@Delete({"delete from goods where id=#{id}"})
int deleteGoods(Object id);
@Select({"select * from goods"})
@Results( id = "goodsMap",value = {
@Result(id = true ,property = "id",column = "id"),
@Result(property = "name",column = "name"),
@Result(property = "category",column = "category_id",one = @One(select = "com.aaa.Dao.CategoryDao.findCategoryById",
fetchType = FetchType.EAGER))
}
)
List<Goods> findAllGoodsWithCate();
@Select({"select * from category where category_id=#{id}"})
List<Goods> findGoodsById(Object id);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!--开启懒加载-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--将积极加载改为按需加载-->
<setting name="aggressiveLazyLoading" value="false"/>
<!-- 开启驼峰命名-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!--解决toString方法触发懒加载-->
<setting name="lazyLoadTriggerMethods" value=""/>
<!-- 开启二级缓存-->
<setting name="cacheEnabled" value="true"/>
</settings>
<!--给实体类起别名 -->
<typeAliases>
<package name="com.aaa.Entity"></package>
</typeAliases>
<!-- 配置Mybatis链接-->
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED"><!-- POOLED意思是使用连接池 -->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/world"/>
<property name="username" value="root"/>
<property name="password" value="476988"/>
</dataSource>
</environment>
</environments>
<!-- mapper信息注册-->
<mappers>
<package name="com.aaa.Dao"/>
</mappers>
</configuration>
Spring
获取容器的方式:
// 获取容器的方式1:这种方式需要给出绝对路径
BeanFactory beanFactory=new XmlBeanFactory(new FileUrlResource("D:\\Java\\万老师的课\\万老师Spring\\AspectJ\\aop\\src\\main\\resources\\aspcetj\\xml\\aspect.xml"));
//获取容器的方式2:相对路径
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:aspcetj/xml/aspect.xml");
//获取容器的方式3:绝对路径
ApplicationContext applicationContext=new FileSystemXmlApplicationContext("D:\\Java\\万老师的课\\万老师Spring\\AspectJ\\aop\\src\\main\\resources\\aspcetj\\xml\\aspect.xml");
@Autowired:用于对Bean的属性变量、属性的setter方法及构造方法进行标注,配合对应的注解处理器完成Bean的自动配置工作。
@Qualifier:与@Autowired注解配合使用,会将默认的按Bean类型装配修改为按Bean的实例名称装配,Bean的实例名称由@Qualifier注解的参数指定。
@Resource:其作用与Autowired一样。@Resource中有两个重要属性:name和type。Spring将name属性解析为Bean实例名称,type属性解析为Bean实例类型。
// @Autowired // 根据类型进行注入若有,多个类型的实例,需要配合 @Qualifier("studentDaoImpl2")
// @Qualifier("studentDaoImpl2")
@Resource() // 默认按照注解的字段名称进行装配, 若没有找到根据类型装配,如果对应类型的实例有多个需要限定
private StudentDao studentDao ;
生成实例的注解
@Component:用于描述Spring中的Bean,它是一个泛化的概念,仅仅表示一个组件。
@Repository:用于将数据访问层(DAO)的类标识为Spring中的Bean 。
@Service:用于将业务层(Service)的类标识为Spring中的Bean。
@Controller:用于将控制层(Controller)的类标识为Spring中的Bean 。
在spring中:
@RequestMapping 是请求的地址;
@ResponseBody是将返回的元素按照json格式返回
@Configuration 表明当前类是一个配置类,相当于bean。xml文件
@RequiresGuest // 要求当前用户时游客,必须是未登录 如果已经登陆则会报错 注意当前/guest 必须设置不拦截 anon
@RequiresAuthentication// 只要当前用户登录就可以查看
@RequiresPermissions("goods:add") // 必须要求访问当前路径,必须有goods:add 权限
@RequiresRoles("boss") // 要求访问当前路径必须是boss
@SpringBootApplication:表明当前类是一个启动类,一个springboot项目只能有一个