各种注解Spring和MYBATIS

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项目只能有一个

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值