赞
踩
一. 动态sql
1. if标签
2. where标签
3. foreach标签
二. 动态sql例子
1. 新建一个名为DynamicSql的Java工程, 拷入相关jar包
2. 新建User.java
- package com.lywgames.domain;
-
- import java.util.Date;
-
- public class User {
- private Integer id;
- private String name;
- private String sex;
- private Date birthday;
- private String address;
-
- public User() {
- }
-
- public User(String name, String sex, Date birthday, String address) {
- this.name = name;
- this.sex = sex;
- this.birthday = birthday;
- this.address = address;
- }
-
- public User(Integer id, String name, String sex, Date birthday, String address) {
- this.id = id;
- this.name = name;
- this.sex = sex;
- this.birthday = birthday;
- this.address = address;
- }
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getSex() {
- return sex;
- }
-
- public void setSex(String sex) {
- this.sex = sex;
- }
-
- public Date getBirthday() {
- return birthday;
- }
-
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
-
- public String getAddress() {
- return address;
- }
-
- public void setAddress(String address) {
- this.address = address;
- }
-
- @Override
- public String toString() {
- return "User [id=" + id + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday + ", address=" + address
- + "]";
- }
-
- }
3. 新建UserMapper.java接口
- package com.lywgames.dao;
-
- import java.util.List;
- import com.lywgames.domain.User;
-
- public interface UserMapper {
- public List<User> selectUserByName(String name);
- public List<User> selectUserByIds(List<Integer> ids);
- public List<User> selectUser(User user);
- }
4. 在和UserMapper.java接口同一目录下新建UserMapper.xml映射文件
5. 在src目录下新建jdbc.properties数据库属性文件
6. 在src目录下新建SqlMapConfig.xml数据库配置文件
7. 新建Test.java
- package com.lywgames;
-
- import java.io.InputStream;
- import java.util.Arrays;
- import java.util.List;
- import org.apache.ibatis.io.Resources;
- import org.apache.ibatis.session.SqlSession;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.apache.ibatis.session.SqlSessionFactoryBuilder;
- import com.lywgames.dao.UserMapper;
- import com.lywgames.domain.User;
-
- public class Test {
- public static void main(String[] args) {
- SqlSession sqlSession = null;
- try {
- // 指定全局配置文件
- String resource = "SqlMapConfig.xml";
- // 读取配置文件
- InputStream inputStream = Resources.getResourceAsStream(resource);
- // 构建sqlSessionFactory
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- // 获取sqlSession
- sqlSession = sqlSessionFactory.openSession();
-
- UserMapper userDao = sqlSession.getMapper(UserMapper.class);
-
- // 查询操作
- List<User> users = userDao.selectUserByName("李");
- for (User user : users) {
- System.out.println(user);
- }
- System.out.println("--------------------------------------");
-
- users = userDao.selectUser(new User(4, "李", "", null, ""));
- for (User user : users) {
- System.out.println(user);
- }
- System.out.println("--------------------------------------");
-
- users = userDao.selectUserByIds(Arrays.asList(4, 5));
- for (User user : users) {
- System.out.println(user);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if(sqlSession != null) {
- // 释放资源
- sqlSession.close();
- }
- }
- }
- }
8. 查看user表数据
9. 运行项目
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。