[随记]Spring一部分注解的使用

来源:互联网 发布:淘宝店铺crm做法 编辑:程序博客网 时间:2024/06/05 00:13

spring配置注解方式管理bean

<!-- Scans the classpath of this application for @Components to deploy as beans -->    <context:component-scan base-package="br.com.braziljs.loiane" />    <!-- Configures the @Controller programming model -->    <mvc:annotation-driven />    <!-- 视图解析 -->    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>        <property name="suffix" value=".jsp"/>    </bean>

实体bean与表的映射

@JsonAutoDetect //自动将json数据转为对象@Entity         //持久化实体@Table(name="CONTACT")  //映射数据表public class Contact {    @Id    @GeneratedValue    @Column(name="CONTACT_ID")    private int id;    @Column(name="CONTACT_NAME", nullable=false)    private String name;    @Column(name="CONTACT_PHONE", nullable=false)    private String phone;    @Column(name="CONTACT_EMAIL", nullable=false)    private String email;    。。。。。省略getter setter方法

控制层相关注解

@Controllerpublic class XxxxController  {    @Autowired    private XxxxService xxxxService;    @RequestMapping(value="/Xxxx/view")    public @ResponseBody Map<String,? extends Object> view(@RequestParam int start, @RequestParam int limit) throws Exception {        try{            List<Xxxx> xxxxs = contactService.getContactList(start,limit);            int total = xxxxxService.getTotalXxxxs();            Map<String,Object> results = new HashMap<>();            results.put("data",xxxxs);            return results;        } catch (Exception e) {            e.printStackTrace();        }    }    @RequestMapping(value="/xxxxx/create")    public @ResponseBody Map<String,? extends Object> create(@RequestBody ContactWrapper data) throws Exception {        try{            List<Xxxx> xxxxs = contactService.getContactList(start,limit);            int total = xxxxxService.getTotalXxxxs();            Map<String,Object> results = new HashMap<>();            results.put("data",xxxxs);            return results;        } catch (Exception e) {            e.printStackTrace();        }    }    }

**@RequestMapping:设置请求响应的url
@RequestParam:将请求中的相应参数直接赋值该变量
@ResponseBody:返回值类型,将键值对数据直接转为json格式数据返回。参数中出现则将json数据直接转为对象,前提该对象是有@JsonAutoDetect注解的。**

0 0