三分钟带你看懂python中*和**的使用

一,*的使用

1.作为乘法运算符来使用

print(3*5)
#输出15

2,(1)作为收集任意数量的参数来使用

a,b,*c=[1,2,3,4]
#a=1  b=2  c=[3,4]

*c会收集剩余的参数并且将其组合为一个列表输出

 (2)定义函数中也可以收集任意数量的实参

def example(example_1,*example_2):
   print(f"{example_1}and the{example_2})
example(a,b,c,d)
example(1,2,3)
#输出“a and the b,c,d"
#输出"1 and the 2,3"

该函数先依据位置实参将a传入example_1,再将剩余数量的实参传入example_2

(3)定义函数时收集参数组成为元组导出

def example(*example_3):
    print(example_3)
example(1,2,3)
example(a)
#输出(1,2,3)
#输出(a,)

3,导入模块中的所有函数

from collections import *

此时可以使用collections模块中的所有函数,但是为了更加便于理解和阅读代码,不建议使用这种方法。

注意:在定义函数使用*example_2时,不要给后面再添加形参,因为python会分不清楚*example_2是否该接收剩下的所有值而报错。

二,**的用法

**用于收集关键字参数从而组成字典输出

def example(**example_4):
    print(example_4)
example("name":"mike","age":18)
#输出"{"name":"mike","age":18}

三,*与**有关拆包的使用

def text_one(a,b,c):
    print(a,b,c)
tu=(1,2,3)
text_one(*tu)
#输出a=1 b=2 c=3

使用*将元组tu拆分再分配给a,b,c

同理

def text_two(*nums, **dict):
    print(nums)
    print(dict)
nums = ('a', "b", "c")
dict = {"name": "mike", "age": 18}
text_two(*nums, **dict)
#输出('a', 'b', 'c')
#{'name': 'mike', 'age': 18}

如果不加星号最终的输出将会变为(("a","b","c"){"name:"mike","age":18}) 

好的,@Mapper注解是Mybatis框架用于标识数据访问层接口的注解,用于告诉Spring容器将该接口类实例化并注入到其他Bean。其使用步骤如下: 1. 在Spring Boot项目引入Mybatis和Mybatis-Spring的依赖 2. 在配置文件配置数据源和Mybatis的相关属性 3. 创建一个数据访问层接口,使用@Mapper注解标识该接口 4. 在该数据访问层接口定义需要操作的数据库方法 5. 在Service或Controller注入该数据访问层接口的实例,并调用其的方法 下面是一个示例: 1. 在pom.xml添加Mybatis和Mybatis-Spring的依赖: ```xml <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> ``` 2. 在application.properties配置数据源和Mybatis的相关属性: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 mybatis.type-aliases-package=com.example.demo.entity mybatis.mapper-locations=classpath:mapper/*.xml ``` 3. 创建一个数据访问层接口UserMapper,使用@Mapper注解标识该接口: ```java @Mapper public interface UserMapper { User selectByPrimaryKey(Integer id); int insert(User record); int updateByPrimaryKey(User record); int deleteByPrimaryKey(Integer id); } ``` 4. 在mapper目录下创建UserMapper.xml,定义需要操作的数据库方法: ```xml <mapper namespace="com.example.demo.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.example.demo.entity.User"> <id column="id" property="id" jdbcType="INTEGER"/> <result column="username" property="username" jdbcType="VARCHAR"/> <result column="password" property="password" jdbcType="VARCHAR"/> </resultMap> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer"> select * from user where id = #{id,jdbcType=INTEGER} </select> <insert id="insert" parameterType="com.example.demo.entity.User" useGeneratedKeys="true" keyProperty="id"> insert into user (username, password) values (#{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}) </insert> <update id="updateByPrimaryKey" parameterType="com.example.demo.entity.User"> update user set username = #{username,jdbcType=VARCHAR}, password = #{password,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER} </update> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> delete from user where id = #{id,jdbcType=INTEGER} </delete> </mapper> ``` 5. 在Service或Controller注入UserMapper的实例,并调用其的方法: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User selectByPrimaryKey(Integer id) { return userMapper.selectByPrimaryKey(id); } @Override public int insert(User user) { return userMapper.insert(user); } @Override public int updateByPrimaryKey(User user) { return userMapper.updateByPrimaryKey(user); } @Override public int deleteByPrimaryKey(Integer id) { return userMapper.deleteByPrimaryKey(id); } } ``` 这就是使用@Mapper注解的基本步骤,希望对你有所帮助。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

下水道程序员

你的鼓励将是我奋斗的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值