首页 > 其他分享 >ibatis 入门例子 增删改查

ibatis 入门例子 增删改查

时间:2023-04-26 22:03:08浏览次数:29  
标签:改查 public ibatis user 增删 import id User


新建maven工程:

ibatis 入门例子 增删改查_List

 

java代码:

package com.ibatis.dao;

import java.util.List;

import com.ibatis.entity.User;

public interface IUserDao {
	void insertUser(User user);

	void deleteUserById(int id);

	void updateUser(User user);

	User selectUserById(int id);

	List<User> selectAllUser();
}

 

package com.ibatis.dao.impl;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;

import com.ibatis.common.resources.Resources;
import com.ibatis.dao.IUserDao;
import com.ibatis.entity.User;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class UserDao implements IUserDao {

	private static SqlMapClient sqlMapClient = null;

	static {
		try {
			Reader reader = Resources.getResourceAsReader("com/ibatis/entity/SqlMapConfig.xml");
			sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void insertUser(User user) {
		try {
			sqlMapClient.insert("addUser", user);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void deleteUserById(int id) {
		try {
			sqlMapClient.delete("deleteUserById", id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void updateUser(User user) {
		try {
			sqlMapClient.update("updateUser", user);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public User selectUserById(int id) {
		User user = null;
		try {
			user = (User) sqlMapClient.queryForObject("selectUserById", id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}

	@SuppressWarnings("unchecked")
	public List<User> selectAllUser() {
		List<User> userList = null;
		try {
			userList = (List<User>) sqlMapClient.queryForList("selectAllUser");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return userList;
	}

}

 

package com.ibatis.entity;

public class User {
	private int id;
	private String username;
	private int age;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", age=" + age + "]";
	}
}

配置文件

com/ibatis/entity/User.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
   "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap>
	<typeAlias alias="User" type="com.ibatis.entity.User" />

	<select id="selectAllUser" resultClass="User">
		select id, user_name username, user_age age from tb_user
	</select>

	<select id="selectUserById" parameterClass="int" resultClass="User">
		select id, user_name username, user_age age from tb_user where id=#id#
	</select>

	<insert id="addUser" parameterClass="User">
		insert into tb_user(id,user_name,user_age) values (#id#, #username#,#age#)
	</insert>

	<delete id="deleteUserById" parameterClass="int">
		delete from tb_user where id=#id#
	</delete>

	<update id="updateUser" parameterClass="User">
		update tb_user set user_name=#username#, user_age=#age# where id=#id#
	</update>

</sqlMap>

com/ibatis/entity/SqlMapConfig.xml:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMapConfig      
    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"      
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>

	<transactionManager type="JDBC" commitRequired="false">
		<dataSource type="SIMPLE">
			<property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver" />
			<property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@localhost:1521:ORCL" />
			<property name="JDBC.Username" value="HIBERNATE" />
			<property name="JDBC.Password" value="HIBERNATE" />
		</dataSource>
	</transactionManager>

	<sqlMap resource="com/ibatis/entity/User.xml" />

</sqlMapConfig>

 

测试类:

package com.ibatis.test;

import java.util.List;

import com.ibatis.dao.IUserDao;
import com.ibatis.dao.impl.UserDao;
import com.ibatis.entity.User;

import junit.framework.TestCase;

public class TestUser extends TestCase {
	private IUserDao userDao =  new UserDao();

	public void testInsert() {
		User user = new User();
		user.setId(1);
		user.setUsername("user1");
		user.setAge(18);
		userDao.insertUser(user);
	}
	
	public void testUpdate() {
		User user = new User();
		user.setId(1);
		user.setUsername("user11");
		user.setAge(18);
		userDao.updateUser(user);
	}
	
	public void testSelectById() {
		User user = userDao.selectUserById(1);
		System.out.println(user);
	}
	
	public void testSelectAllUser() {
		List<User> userList = userDao.selectAllUser();
		for (User user: userList) {
			System.out.println(user);
		}
	}
	
	public void testDelete() {
		userDao.deleteUserById(1);
	}
}

 

package com.ibatis.test;

import junit.framework.Test;
import junit.framework.TestSuite;

public class TestAll {
	public static Test suite() {
		TestSuite suite = new TestSuite(TestUser.class.getSimpleName());
		suite.addTestSuite(TestUser.class);
		return suite;
	}
}

pom文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>ibatis</groupId>
	<artifactId>ibatis</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>org.apache.ibatis</groupId>
			<artifactId>ibatis-core</artifactId>
			<version>3.0</version>
		</dependency>

		<dependency>
			<groupId>org.apache.ibatis</groupId>
			<artifactId>ibatis-sqlmap</artifactId>
			<version>2.3.4.726</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.8.1</version>
			<scope>test</scope>
		</dependency>
		
		<dependency>
			<groupId>com.oracle</groupId>
			<artifactId>ojdbc14</artifactId>
			<version>10.2.0.1.0</version>
		</dependency>

	</dependencies>
</project>

SQL:

CREATE TABLE TB_USER
(
    ID INTEGER PRIMARY KEY,
    USER_NAME VARCHAR2(20) NOT NULL,
    USER_AGE INTEGER NOT NULL
);

 

标签:改查,public,ibatis,user,增删,import,id,User
From: https://blog.51cto.com/u_1002776/6229001

相关文章

  • ORM执行SQL语句,双下__查询,多表查询,外建字段增删改查
    目录ORM执行SQL语句查看原生sql语句神奇的双下划线查询ORM外键字段的创建一对多的外键增删改查数据多对多外键增删改查ORM跨表查询基于对象的跨表查询基于上下划线的跨表查询进阶操作ORM执行SQL语句有时候ORM的操作效率可能偏低我们是可以自己编写SQL的方式1: models.User.o......
  • Django模型层(一) (测试环境搭配 常见的十几种查询方法-ORM关键字 ORM执行SQL语句
    目录一、测试环境搭配切换数据库自带的sqlite3数据库对时间字段不敏感有时候会展示错乱,所以我们习惯切换成常见的数据库比如MySQLdjangoorm并不会自动帮你创建库,所以需要提前准备好!单独搭配测试环境单独测试django某个功能层,默认不允许单独测试某个py文件,如果想要测试......
  • Service层报错org.apache.ibatis.binding.BindingException: Invalid bound statement
    如果在主启动类配置了包扫描@MapperScan注解,这个位置目录一定要写到dao层的目录,如:@MapperScan(“com.company.module.dao”)如果没有写到具体的dao层目录,写成@MapperScan(“com.company”)扫描的时候会把com.company.module.service目录也认为是dao目录扫进来,这时用到@Service注解......
  • ibatis 多条件查询
    1、在java代码中使用字符串拼接映射文件中这样写:Xml代码1.<selectid="getBrno"parameterClass="java.lang.String"resultMap="bctlResult">2.selectbrno,brnamefrombctlwhere$sql$3.</select>然后再java文件中拼写出SQL的......
  • Geotools基本增删改查Feature
    postgis依赖<dependency><groupId>org.geotools</groupId><artifactId>gt-main</artifactId><version>27.2</version></dependency><dependency><groupId>org.geotools</groupId>......
  • elasticsearch——增删改查
    --创建索引PUT/icon_index{"settings":{"analysis":{"analyzer":{"text_anlyzer":{"tokenizer":"ik_max_word","filter":"py"......
  • django前后端连接数据库的增删查改
    目录配置修改1.templates目录修改路径2.如果要添加新的应用的话则需要在installed_apps里面加上去3.static静态文件的配置成动态文件4.连接数据库5.前期发送post请求需要注释掉配置文件中的某一行urls.py存储网址后缀与函数名对应的关系(开设接口)数据库数据数据展现功能1.t......
  • 洪君:mybatis plus012:增删改查 洪君
    plus的pom依赖:替代原mybatis<!--mybatisplus--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus</artifactId><version>2.1.9</version></d......
  • C#写一套最全的SQL server帮助类(包括增删改查)
    我定义了一系列静态方法,用于执行SQLServer数据库的增删改查等操作。其中:ExecuteNonQuery方法用于执行指定的SQL语句并返回受影响的行数;ExecuteScalar方法用于执行指定的SQL语句并返回查询结果的第一行第一列;ExecuteDataTable方法用于执行指定的SQL语句并返回一个数据表;ExecuteRea......
  • C#写一套最全的MySQL帮助类(包括增删改查)
    介绍说明:这个帮助类包含了六个主要的方法:ExecuteNonQuery、ExecuteScalar、ExecuteQuery、ExecuteQuery(泛型)、Insert、Update和Delete。其中,ExecuteNonQuery用于执行不返回结果集的SQL语句;ExecuteScalar用于执行一个查询,并返回结果集中第一行的第一列;ExecuteQuery用于执行一个查询......