Spring笔记4---<util/>、<p/>命名空间

本文介绍了Spring框架中几种实用的工具元素,包括&lt;util:constant/&gt;、&lt;util:property-path/&gt;、&lt;util:properties/&gt;、&lt;util:list/&gt;、&lt;util:map/&gt;和&lt;util:set/&gt;的使用方法及示例。同时展示了如何利用&p命名空间简化配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. <util:constant/>元素

 

比如某类存在如下字段定义

 

 

public static final String hwStatic = "hello static";

public String hw = "hw";

 

如果希望以上属性取值作为受管Bean,可以如下配置:

 

 

<util:constant id="hwConstant" static-field="test.HelloWorld.hwStatic"/>

 

 

2. <util:property-path/>元素

 

 

<util:property-path id="property-path" path="helloWorld.hello"/>
<bean id="helloWorld" class=''test.HelloWorld">
<property name="hello" value="hi"/>
</bean>

 

 

3. <util:properties/>元素

 

    "classpath:"表明,将从类路径上查找并装载xxx属性文件. 

 

 

<util:properties id="xxx" location="classpath:xxxxx.properties"> 

 

 

<util:list id="listUtil" list-class="java.util.ArrayList">
<value>first</valuse>
<value>two</valuse>
<value>three</valuse>
<value>ten</valuse>
</util:list>
 

 

4. <util:map/>元素

 

 

 

<bean id="abstractCollectionBean" abstract="true">
<property name="map">
<map>
<entry key="mapKey1" value="mapValue1">
<entry key="mapKey2" value="mapValue2">
</map>
</property>

</bean>

 

 

    继承了abstractCollectionBean的子bean

<bean id="CollectionBean"  class="test.CollectionBean" parent="abstractCollectionBean">
<property name="map">
<map merge="true" key-type="java.lang.String" value-type="java.lang.String">
<entry key="mapKey1" value="mapValue1Override"/>
<entry>
  <key><value>mapKey2</value></key>
  <value>mapValue2</value>
</entry>
<entry key="testBean" value-ref="testBean">
</map>
</property>

</bean>
 <bean id="testBean" class="test.TestBean" />
 

 

    为了简化MapFactoryBean对象的使用,可使用如下代码

 

<util:map id="mapUtil" map-class="java.util.HashMap">
<entry key="1" value="first">
<entry key="2" value="two">
<entry key="3" value="three">
</util:map>
 

 

5. <util:set/>元素

 

   同样的,为了简化SetFactoryBean对象,可使用如下代码

 

 

<util:set id="setUtil" set-class="java.util.HashSet">
<value>first</value>
<value>two</value>
<value>three</value>
</util:set>

 

6. 使用<p/>命名空间

 

    在xml头加入

 

 xmlns:p="http://www.springframework.org/schema/p"

  

 

    例如如下代码:

 

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" ref="locations"/>
<property name="order" value="1"/>

</bean>

<util:list id="locations">
<value>userinfo.properties</value>

</util:list>

 

    在导入了</p>命名空间后,等价于

 

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"  p:locations-ref="locations" p:order="1" />


<util:list id="locations">
<value>userinfo.properties</value>

</util:list>
题目:基于SpringBoot+Mysql+MyBatis-Plus实现简单商城项目(满分100分) 说明:制作后端有关于商品表的增、删、改、查接口以及用户登录接口并通过ApiFox测试接口功能是否齐全。 要求: 1、使用SpringBoot+Maven搭建后端项目。 2、连接MySQL数据库,创建对应数据库数据表。 3、基于Mybatis-Plus实现用户登录接口以及对应商品的增、删、改、查功能接口。 4、使用ApiFox验证接口功能完整性。 5、Apifox验证的每个接口效果截图统一存放在文件夹中(注:ApiFox需要看到完整请求路径以及后端返回的响应体数据)。 6、文件夹命名格式要求为学号+姓名 提交的内容有项目工程源码、效果截图。 7.用户表商品表如下表所示 请根据提供的字段来设计数据表,数据库名为自己名字拼音缩写 用户表 字段名 类型 描述 user_id INT 主键ID(填充数据时 填充自己的学号后两位) Password VARCHAR(50) 密码 username VARCHAR(50) 用户名(填充数据时 填充自己的姓名) is_active TINYINT(1) 账户状态(1启用/0禁用) 商品表 字段名 类型 描述 product_id INT 主键,自增商品ID product_name VARCHAR(100) 商品名称 price VARCHAR(50) 价格 is_available INT 上架状态(1在售/0下架) 评分标准:(满分100分) (1)使用SpringBoot+Maven搭建后端项目(20分); (2)MySQL设计实现的数据表齐全 (10分); (3)SpringBoot实现用户登录功能 (15分); (4SpringBoot实现商品的增、删、改、查的功能(20分); (5)使用Apifox测试对应接口功能 (15分); (6)项目按照题目以及说明实现,截图清晰完整(20分); 根据以下代码搭建前端: 代码: Src: Main: Java: Com.example.mall: Controller: ProductController: package com.example.mall.controller; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.example.mall.entity.Product; import com.example.mall.entity.SaResult; import com.example.mall.service.ProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/product") public class ProductController { private final ProductService productService; @Autowired public ProductController(ProductService productService) { this.productService = productService; } @PostMapping public SaResult add(@RequestBody Product product) { boolean result = productService.save(product); return result ? SaResult.data(product) : SaResult.error("添加失败"); } @DeleteMapping("/{id}") public SaResult delete(@PathVariable Integer id) { boolean result = productService.removeById(id); return result ? SaResult.ok("删除成功") : SaResult.error("删除失败"); } @PutMapping public SaResult update(@RequestBody Product product) { boolean result = productService.updateById(product); return result ? SaResult.data(product) : SaResult.error("更新失败"); } @GetMapping("/{id}") public SaResult getById(@PathVariable Integer id) { Product product = productService.getById(id); return product != null ? SaResult.data(product) : SaResult.error("商品不存在"); } @GetMapping("/list") public SaResult list() { return SaResult.data(productService.list()); } @GetMapping("/page") public SaResult page(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) { Page<Product> page = new Page<>(pageNum, pageSize); return SaResult.data(productService.page(page)); } } UserControllrt: package com.example.mall.controller; import com.example.mall.entity.SaResult; import com.example.mall.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/user") public class UserController { private final UserService userService; @Autowired public UserController(UserService userService) { this.userService = userService; } @PostMapping("/login") public SaResult login(@RequestParam String username, @RequestParam String password) { return userService.login(username, password); } } Dao: ProductMapper: package com.example.mall.dao; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.mall.entity.Product; import org.apache.ibatis.annotations.Mapper; @Mapper public interface ProductMapper extends BaseMapper<Product> { } Entity: Product: package com.example.mall.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @Data @TableName("product") public class Product { @TableId(type = IdType.AUTO) private Integer productId; private String productName; private String price; private Integer isAvailable; } SaResult: package com.example.mall.entity; import java.io.Serializable; import java.util.LinkedHashMap; public class SaResult extends LinkedHashMap<String, Object> implements Serializable { private static final long serialVersionUID = 1L; public static final int CODE_SUCCESS = 200; public static final int CODE_ERROR = 500; public SaResult() { } public SaResult(int code, String msg, Object data) { this.setCode(code); this.setMsg(msg); this.setData(data); } // 添加setter方法 public void setCode(int code) { this.put("code", code); } public void setMsg(String msg) { this.put("msg", msg); } public void setData(Object data) { this.put("data", data); } public static SaResult ok() { return new SaResult(CODE_SUCCESS, "ok", null); } public static SaResult ok(String msg) { return new SaResult(CODE_SUCCESS, msg, null); } public static SaResult data(Object data) { return new SaResult(CODE_SUCCESS, "ok", data); } public static SaResult error() { return new SaResult(CODE_ERROR, "error", null); } public static SaResult error(String msg) { return new SaResult(CODE_ERROR, msg, null); } } User: package com.example.mall.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @Data @TableName("user") public class User { @TableId(type = IdType.INPUT) private Integer userId; private String password; private String username; private Integer isActive; } Service: UserServicelmpl: ProductServicelmpl: package com.example.mall.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.example.mall.dao.ProductMapper; import com.example.mall.entity.Product; import com.example.mall.service.ProductService; import org.springframework.stereotype.Service; @Service public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService { } UserServicelmpl: package com.example.mall.service.UserServiceImpl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.example.mall.dao.UserMapper; import com.example.mall.entity.SaResult; import com.example.mall.entity.User; import com.example.mall.service.UserService; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService { private final UserMapper userMapper; public UserServiceImpl(UserMapper userMapper) { this.userMapper = userMapper; } @Override public SaResult login(String username, String password) { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("username", username) .eq("password", password); User user = userMapper.selectOne(queryWrapper); if (user == null) { return SaResult.error("用户名或密码错误"); } if (user.getIsActive() == 0) { return SaResult.error("账号已被禁用"); } return SaResult.data(user); } } ProductService: package com.example.mall.service; import com.baomidou.mybatisplus.extension.service.IService; import com.example.mall.entity.Product; public interface ProductService extends IService<Product> { } UserService: package com.example.mall.service; import com.example.mall.entity.SaResult; import com.example.mall.entity.User; public interface UserService { SaResult login(String username, String password); } MallApplication: package com.example.mall; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MallApplication { public static void main(String[] args) { SpringApplication.run(MallApplication.class, args); } } Resources: Application.yml server: port: 8080 servlet: context-path: /api spring: datasource: url: jdbc:mysql://localhost:3306/huangshuhua_db username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
最新发布
06-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值