tinyint(1)和tinyint(4)和int(11)的区别和用法


1 bytes = 8 bit ,一个字节最多可以代表的数据长度是2的8次方 11111111 在计算机中也就是

-128到127

1.BIT[M]

位字段类型,M表示每个值的位数,范围从1到64,如果M被忽略,默认为1

2.TINYINT[(M)] [UNSIGNED] [ZEROFILL]  M默认为4

很小的整数。带符号的范围是-128到127。无符号的范围是0到255。

3. BOOL,BOOLEAN

是TINYINT(1)的同义词。zero值被视为假。非zero值视为真。

4.SMALLINT[(M)] [UNSIGNED] [ZEROFILL] M默认为6

小的整数。带符号的范围是-32768到32767。无符号的范围是0到65535。

5.MEDIUMINT[(M)] [UNSIGNED] [ZEROFILL] M默认为9

中等大小的整数。带符号的范围是-8388608到8388607。无符号的范围是0到16777215。

6. INT[(M)] [UNSIGNED] [ZEROFILL]   M默认为11

普通大小的整数。带符号的范围是-2147483648到2147483647。无符号的范围是0到4294967295。

7.BIGINT[(M)] [UNSIGNED] [ZEROFILL] M默认为20

大整数。带符号的范围是-9223372036854775808到9223372036854775807。无符号的范围是0到18446744073709551615。

注意:这里的M代表的并不是存储在数据库中的具体的长度,以前总是会误以为int(3)只能存储3个长度的数字,int(11)就会存储11个长度的数字,这是大错特错的。

tinyint(1) 和 tinyint(4) 中的1和4并不表示存储长度,只有字段指定zerofill是有用,
如tinyint(4),如果实际值是2,如果列指定了zerofill,查询结果就是0002,左边用0来填充。

 

转自:https://blog.csdn.net/jaryle/article/details/52025023

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Spring Security和MySQL进行身份验证,您需要完成以下步骤: 1. 添加Spring Security和MySQL的依赖。您可以在Maven或Gradle中添加以下依赖: ```xml <!-- Spring Security --> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>5.5.1</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>5.5.1</version> </dependency> <!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency> ``` 2. 创建一个用户表。您需要在MySQL中创建一个用户表,用于存储用户信息和密码。例如: ```sql CREATE TABLE users ( id INT NOT NULL AUTO_INCREMENT, username VARCHAR(50) NOT NULL, password VARCHAR(100) NOT NULL, enabled TINYINT NOT NULL, PRIMARY KEY (id), UNIQUE KEY unique_username (username) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ``` 3. 实现UserDetailsService接口。您需要创建一个实现UserDetailsService接口的类,用于查询用户信息并返回UserDetails对象。例如: ```java @Service public class UserDetailsServiceImpl implements UserDetailsService { @Autowired private UserRepository userRepository; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = userRepository.findByUsername(username); if (user == null) { throw new UsernameNotFoundException("User not found"); } List<GrantedAuthority> authorities = new ArrayList<>(); for (Role role : user.getRoles()) { authorities.add(new SimpleGrantedAuthority(role.getName())); } return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), user.isEnabled(), true, true, true, authorities); } } ``` 其中,UserRepository是一个自定义的JpaRepository,用于从数据库中查询用户信息。User类包含了用户名、密码、是否可用等信息,以及一个Role列表,用于存储此用户所拥有的角色。 4. 配置Spring Security。您需要在Spring Security的配置文件中指定UserDetailsService和PasswordEncoder,并配置身份验证规则。例如: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/admin/**").hasRole("ADMIN") .antMatchers("/user/**").hasAnyRole("USER", "ADMIN") .antMatchers("/", "/login").permitAll() .and() .formLogin() .loginPage("/login") .defaultSuccessUrl("/") .and() .logout() .logoutUrl("/logout") .logoutSuccessUrl("/") .and() .csrf().disable(); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } } ``` 在上述配置中,我们使用了BCryptPasswordEncoder作为PasswordEncoder的实现类,用于对用户密码进行加密。在configure(HttpSecurity http)方法中,我们设置了不同URL需要的角色权限,并指定了登录页、登出URL等相关信息。 5. 创建登录页和主页。最后,您需要创建一个登录页和主页,用于用户登录和展示相关信息。例如: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Login</title> </head> <body> <h1>Login</h1> <form method="post" action="/login"> <div> <label>Username:</label> <input type="text" name="username"> </div> <div> <label>Password:</label> <input type="password" name="password"> </div> <button type="submit">Login</button> </form> </body> </html> ``` ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Home</title> </head> <body> <h1>Welcome!</h1> <p>Logged in as: <strong><sec:authentication property="name"/></strong></p> <p>Roles: <strong><sec:authentication property="authorities"/></strong></p> <a href="/logout">Logout</a> </body> </html> ``` 以上就是使用Spring Security和MySQL进行身份验证的基本步骤。您可以根据自己的需求进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值