User2 queryByParam(@Param("id") Integer id,@Param("name") String name); 请问xml怎么写
时间: 2024-03-02 18:50:04 浏览: 58
您好!根据您提供的代码,以下是对应的Mybatis XML写法:
```xml
<select id="queryByParam" parameterType="map" resultType="User2">
SELECT * FROM user_table
WHERE id = #{id,jdbcType=INTEGER}
AND name = #{name,jdbcType=VARCHAR}
</select>
```
在XML中,我们使用`<select>`标签来定义一个查询语句,其中`id`属性指定该查询语句的唯一标识符,`parameterType`属性指定参数类型,`resultType`属性指定返回结果类型。
在SQL语句中,我们使用`#{}`来定义参数占位符,其中`id`和`name`分别对应方法中的`@Param`注解中定义的参数名。
希望能够帮助到您!
相关问题
1、创建数据库和数据表(以Mysql数据库为例) 设计一个权限管理系统。 表设计如下:用户表、角色表、菜单表 使用Mybaits构建用户、角色、菜单的映射关系 1、用户表(UserInfo):Id、UserName、UserPwd,userSex,userSchool,userAddress 2、角色表(RoleInfo):Id、RoleName 3、菜单表(MenuInfo):Id、MenuName 4、用户角色表(UserRole):Id、UserId、RoleId; 5、角色菜单表(RoleMenu):Id、RoleId、MenuId 2、1.用户和角色之间的关系 2.角色和菜单之间的关系查询编写单元测试用例。 3.使用动态sql拼接的方式实现在一个接口内进行用户名、用户地址、用户性别的筛选。
1. 创建数据库和数据表(以Mysql数据库为例)
创建数据库:
```
CREATE DATABASE IF NOT EXISTS `permission_management` DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
```
创建用户表:
```
CREATE TABLE IF NOT EXISTS `user_info` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
`user_name` varchar(255) NOT NULL COMMENT '用户名称',
`user_pwd` varchar(255) NOT NULL COMMENT '用户密码',
`user_sex` varchar(255) NOT NULL COMMENT '用户性别',
`user_school` varchar(255) NOT NULL COMMENT '用户学校',
`user_address` varchar(255) NOT NULL COMMENT '用户地址',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='用户表';
```
创建角色表:
```
CREATE TABLE IF NOT EXISTS `role_info` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '角色ID',
`role_name` varchar(255) NOT NULL COMMENT '角色名称',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='角色表';
```
创建菜单表:
```
CREATE TABLE IF NOT EXISTS `menu_info` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '菜单ID',
`menu_name` varchar(255) NOT NULL COMMENT '菜单名称',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='菜单表';
```
创建用户角色表:
```
CREATE TABLE IF NOT EXISTS `user_role` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户角色ID',
`user_id` int(11) NOT NULL COMMENT '用户ID',
`role_id` int(11) NOT NULL COMMENT '角色ID',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='用户角色表';
```
创建角色菜单表:
```
CREATE TABLE IF NOT EXISTS `role_menu` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '角色菜单ID',
`role_id` int(11) NOT NULL COMMENT '角色ID',
`menu_id` int(11) NOT NULL COMMENT '菜单ID',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='角色菜单表';
```
2. 使用Mybaits构建用户、角色、菜单的映射关系
创建用户表对应的实体类:
```
public class UserInfo {
private Integer id;
private String userName;
private String userPwd;
private String userSex;
private String userSchool;
private String userAddress;
// getter and setter
}
```
创建角色表对应的实体类:
```
public class RoleInfo {
private Integer id;
private String roleName;
// getter and setter
}
```
创建菜单表对应的实体类:
```
public class MenuInfo {
private Integer id;
private String menuName;
// getter and setter
}
```
创建用户角色表对应的实体类:
```
public class UserRole {
private Integer id;
private Integer userId;
private Integer roleId;
// getter and setter
}
```
创建角色菜单表对应的实体类:
```
public class RoleMenu {
private Integer id;
private Integer roleId;
private Integer menuId;
// getter and setter
}
```
创建用户、角色、菜单的Mapper类:
```
public interface UserInfoMapper {
UserInfo getUserInfoById(Integer id);
List<UserInfo> getUserInfoListByRoleId(Integer roleId);
List<UserInfo> getUserInfoListByUserNameAndUserAddressAndUserSex(@Param("userName") String userName, @Param("userAddress") String userAddress, @Param("userSex") String userSex);
}
public interface RoleInfoMapper {
RoleInfo getRoleInfoById(Integer id);
List<RoleInfo> getRoleInfoListByUserId(Integer userId);
}
public interface MenuInfoMapper {
MenuInfo getMenuInfoById(Integer id);
List<MenuInfo> getMenuInfoListByRoleId(Integer roleId);
}
public interface UserRoleMapper {
UserRole getUserRoleById(Integer id);
List<UserRole> getUserRoleListByUserId(Integer userId);
List<UserRole> getUserRoleListByRoleId(Integer roleId);
}
public interface RoleMenuMapper {
RoleMenu getRoleMenuById(Integer id);
List<RoleMenu> getRoleMenuListByRoleId(Integer roleId);
List<RoleMenu> getRoleMenuListByMenuId(Integer menuId);
}
```
在Mybatis的配置文件中,分别加入上述Mapper类的配置。
3. 用户和角色之间的关系查询
查询某个用户所具有的角色:
```
SELECT r.* FROM role_info r LEFT JOIN user_role ur ON r.id = ur.role_id WHERE ur.user_id = #{userId}
```
查询某个角色所对应的用户列表:
```
SELECT u.* FROM user_info u LEFT JOIN user_role ur ON u.id = ur.user_id WHERE ur.role_id = #{roleId}
```
4. 角色和菜单之间的关系查询
查询某个角色所具有的菜单:
```
SELECT m.* FROM menu_info m LEFT JOIN role_menu rm ON m.id = rm.menu_id WHERE rm.role_id = #{roleId}
```
查询某个菜单所对应的角色列表:
```
SELECT r.* FROM role_info r LEFT JOIN role_menu rm ON r.id = rm.role_id WHERE rm.menu_id = #{menuId}
```
5. 编写单元测试用例
创建测试类:
```
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class PermissionManagementTest {
@Autowired
private UserInfoMapper userInfoMapper;
@Autowired
private RoleInfoMapper roleInfoMapper;
@Autowired
private MenuInfoMapper menuInfoMapper;
@Autowired
private UserRoleMapper userRoleMapper;
@Autowired
private RoleMenuMapper roleMenuMapper;
@Test
public void testGetUserInfoById() {
UserInfo userInfo = userInfoMapper.getUserInfoById(1);
System.out.println(userInfo.getUserName());
}
@Test
public void testGetRoleInfoById() {
RoleInfo roleInfo = roleInfoMapper.getRoleInfoById(1);
System.out.println(roleInfo.getRoleName());
}
@Test
public void testGetMenuInfoById() {
MenuInfo menuInfo = menuInfoMapper.getMenuInfoById(1);
System.out.println(menuInfo.getMenuName());
}
@Test
public void testGetUserInfoListByRoleId() {
List<UserInfo> userInfoList = userInfoMapper.getUserInfoListByRoleId(1);
for (UserInfo userInfo : userInfoList) {
System.out.println(userInfo.getUserName());
}
}
@Test
public void testGetRoleInfoListByUserId() {
List<RoleInfo> roleInfoList = roleInfoMapper.getRoleInfoListByUserId(1);
for (RoleInfo roleInfo : roleInfoList) {
System.out.println(roleInfo.getRoleName());
}
}
@Test
public void testGetMenuInfoListByRoleId() {
List<MenuInfo> menuInfoList = menuInfoMapper.getMenuInfoListByRoleId(1);
for (MenuInfo menuInfo : menuInfoList) {
System.out.println(menuInfo.getMenuName());
}
}
@Test
public void testGetUserRoleListByUserId() {
List<UserRole> userRoleList = userRoleMapper.getUserRoleListByUserId(1);
for (UserRole userRole : userRoleList) {
System.out.println(userRole.getRoleId());
}
}
@Test
public void testGetUserRoleListByRoleId() {
List<UserRole> userRoleList = userRoleMapper.getUserRoleListByRoleId(1);
for (UserRole userRole : userRoleList) {
System.out.println(userRole.getUserId());
}
}
@Test
public void testGetRoleMenuListByRoleId() {
List<RoleMenu> roleMenuList = roleMenuMapper.getRoleMenuListByRoleId(1);
for (RoleMenu roleMenu : roleMenuList) {
System.out.println(roleMenu.getMenuId());
}
}
@Test
public void testGetRoleMenuListByMenuId() {
List<RoleMenu> roleMenuList = roleMenuMapper.getRoleMenuListByMenuId(1);
for (RoleMenu roleMenu : roleMenuList) {
System.out.println(roleMenu.getRoleId());
}
}
}
```
6. 使用动态sql拼接的方式实现在一个接口内进行用户名、用户地址、用户性别的筛选
在UserInfoMapper接口中添加如下方法:
```
List<UserInfo> getUserInfoListByCondition(@Param("userName") String userName, @Param("userAddress") String userAddress, @Param("userSex") String userSex);
```
在UserInfoMapper.xml中添加如下SQL:
```
<select id="getUserInfoListByCondition" parameterType="java.util.Map" resultType="com.example.demo.model.UserInfo">
SELECT *
FROM user_info
WHERE 1=1
<if test="userName != null and userName != ''">
AND user_name = #{userName}
</if>
<if test="userAddress != null and userAddress != ''">
AND user_address = #{userAddress}
</if>
<if test="userSex != null and userSex != ''">
AND user_sex = #{userSex}
</if>
</select>
```
在UserService中调用该方法:
```
public List<UserInfo> getUserInfoListByCondition(String userName, String userAddress, String userSex) {
return userInfoMapper.getUserInfoListByCondition(userName, userAddress, userSex);
}
```
运用spring boot框架用@Controller @ResponseBody@Autowired@PostMapping@Service@Mapper@Data注解和xml编写sql语句实现用户登录案例
好的,下面给出一个简单的用户登录案例,使用了Spring Boot框架、MyBatis、MySQL数据库。
首先,需要在pom.xml文件中添加相关依赖:
```xml
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
</dependencies>
```
然后,在application.properties文件中配置数据库连接信息:
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/db_name?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations=classpath:mapper/*.xml
```
接下来,创建一个User实体类:
```java
@Data
public class User {
private Integer id;
private String username;
private String password;
}
```
然后,在resources目录下创建mapper/UserMapper.xml文件,编写SQL语句:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
<resultMap id="userMap" type="com.example.demo.entity.User">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
</resultMap>
<select id="findByUsernameAndPassword" resultMap="userMap">
select * from user where username=#{username} and password=#{password}
</select>
</mapper>
```
然后,在com.example.demo.mapper包下创建UserMapper接口:
```java
@Mapper
public interface UserMapper {
User findByUsernameAndPassword(@Param("username") String username, @Param("password") String password);
}
```
接着,在com.example.demo.service包下创建UserService接口和实现类UserServiceImpl:
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User findByUsernameAndPassword(String username, String password) {
return userMapper.findByUsernameAndPassword(username, password);
}
}
```
最后,在com.example.demo.controller包下创建UserController:
```java
@Controller
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/login")
@ResponseBody
public String login(String username, String password) {
User user = userService.findByUsernameAndPassword(username, password);
if (user == null) {
return "登录失败";
} else {
return "登录成功";
}
}
}
```
这样,我们就完成了一个简单的用户登录案例。需要注意的是,这里只是一个简单的示例,实际应用中还需要进行参数校验、异常处理等。
阅读全文