当前位置:   article > 正文

05_动态sql_动态sql与关联映射习题

动态sql与关联映射习题

一. 动态sql

1. if标签

2. where标签

3. foreach标签

二. 动态sql例子

1. 新建一个名为DynamicSql的Java工程, 拷入相关jar包

2. 新建User.java

  1. package com.lywgames.domain;
  2. import java.util.Date;
  3. public class User {
  4. private Integer id;
  5. private String name;
  6. private String sex;
  7. private Date birthday;
  8. private String address;
  9. public User() {
  10. }
  11. public User(String name, String sex, Date birthday, String address) {
  12. this.name = name;
  13. this.sex = sex;
  14. this.birthday = birthday;
  15. this.address = address;
  16. }
  17. public User(Integer id, String name, String sex, Date birthday, String address) {
  18. this.id = id;
  19. this.name = name;
  20. this.sex = sex;
  21. this.birthday = birthday;
  22. this.address = address;
  23. }
  24. public Integer getId() {
  25. return id;
  26. }
  27. public void setId(Integer id) {
  28. this.id = id;
  29. }
  30. public String getName() {
  31. return name;
  32. }
  33. public void setName(String name) {
  34. this.name = name;
  35. }
  36. public String getSex() {
  37. return sex;
  38. }
  39. public void setSex(String sex) {
  40. this.sex = sex;
  41. }
  42. public Date getBirthday() {
  43. return birthday;
  44. }
  45. public void setBirthday(Date birthday) {
  46. this.birthday = birthday;
  47. }
  48. public String getAddress() {
  49. return address;
  50. }
  51. public void setAddress(String address) {
  52. this.address = address;
  53. }
  54. @Override
  55. public String toString() {
  56. return "User [id=" + id + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday + ", address=" + address
  57. + "]";
  58. }
  59. }

3. 新建UserMapper.java接口

  1. package com.lywgames.dao;
  2. import java.util.List;
  3. import com.lywgames.domain.User;
  4. public interface UserMapper {
  5. public List<User> selectUserByName(String name);
  6. public List<User> selectUserByIds(List<Integer> ids);
  7. public List<User> selectUser(User user);
  8. }

4. 在和UserMapper.java接口同一目录下新建UserMapper.xml映射文件

5. 在src目录下新建jdbc.properties数据库属性文件

6. 在src目录下新建SqlMapConfig.xml数据库配置文件

7. 新建Test.java

  1. package com.lywgames;
  2. import java.io.InputStream;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import org.apache.ibatis.io.Resources;
  6. import org.apache.ibatis.session.SqlSession;
  7. import org.apache.ibatis.session.SqlSessionFactory;
  8. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  9. import com.lywgames.dao.UserMapper;
  10. import com.lywgames.domain.User;
  11. public class Test {
  12. public static void main(String[] args) {
  13. SqlSession sqlSession = null;
  14. try {
  15. // 指定全局配置文件
  16. String resource = "SqlMapConfig.xml";
  17. // 读取配置文件
  18. InputStream inputStream = Resources.getResourceAsStream(resource);
  19. // 构建sqlSessionFactory
  20. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  21. // 获取sqlSession
  22. sqlSession = sqlSessionFactory.openSession();
  23. UserMapper userDao = sqlSession.getMapper(UserMapper.class);
  24. // 查询操作
  25. List<User> users = userDao.selectUserByName("李");
  26. for (User user : users) {
  27. System.out.println(user);
  28. }
  29. System.out.println("--------------------------------------");
  30. users = userDao.selectUser(new User(4, "李", "", null, ""));
  31. for (User user : users) {
  32. System.out.println(user);
  33. }
  34. System.out.println("--------------------------------------");
  35. users = userDao.selectUserByIds(Arrays.asList(4, 5));
  36. for (User user : users) {
  37. System.out.println(user);
  38. }
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. } finally {
  42. if(sqlSession != null) {
  43. // 释放资源
  44. sqlSession.close();
  45. }
  46. }
  47. }
  48. }

8. 查看user表数据

9. 运行项目

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/241547
推荐阅读
相关标签
  

闽ICP备14008679号