版权声明:本文为博主原创文章,未经博主允许不得转载。
package com.zxh.customer.bean1;
import Java.util.List;
import com.zxh.customer.bean.Parter;
public class User {
private Long custId;
private String custName;
private Parter parter;
private List<Parter> parterList;
public Long getCustId() {
return custId;
}
public void setCustId(Long custId) {
this.custId = custId;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public Parter getParter() {
return parter;
}
public void setParter(Parter parter) {
this.parter = parter;
}
public List<Parter> getParterList() {
return parterList;
}
public void setParterList(List<Parter> parterList) {
this.parterList = parterList;
}
}
<?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.zxh.customer.dao1.IUserDao">
<select id="selectUserById" parameterType="long" resultType="User1">
SELECT CUST_ID as "custId", CUST_NAME "custName" FROM CUST WHERE
CUST_ID = #{id}
</select>
<!-- 注意parameterType=int,long,byte这些基本类型 {}里面参数任写, 如果是对象类型必须和属性相同并提供get和set方法
string也是对像类型 -->
<!-- like '%${custName}%' 可以写成 like #{custName} 在程序中把%拼接到custName -->
<select id="selectUsersByName" parameterType="User1" resultType="User1">
SELECT CUST_ID as "custId", CUST_NAME "custName" FROM CUST WHERE
CUST_NAME like '%${custName}%'
</select>
<insert id="saveUser" parameterType="User1">
insert into cust(cust_id,
cust_name) values(seq_cust.nextval, #{custName})
</insert>
<!-- 更新 -->
<update id="updateUser" parameterType="User1">
update cust set cust_name = #{custName} where cust_id = #{custId}
</update>
<!-- 删除 -->
<delete id="deleteUser" parameterType="int">
delete from cust where
cust_id = #{custId}
</delete>
</mapper>
package com.zxh.customer.dao1;
import java.util.List;
import com.zxh.customer.bean1.User;
public interface IUserDao {
// 方法名要和SQL映射文件中的id相同 包名+接口名与mapper的namespace相同
public User selectUserById(Long custId);
public List<User> selectUsersByName(User user);
public void saveUser(User user);
public void updateUser(User user);
public void deleteUser(Long custId);
}
package com.zxh.customer.util1;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MybatisUtil {
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader;
static {
try {
reader = Resources.getResourceAsReader("config1/Configuration.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (Exception e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSessionFactory() {
return sqlSessionFactory;
}
}
Configuration.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 属性配置 xml文件中使用 ${} url "/"路径-文件 或 "."-java类文件 -->
<properties resource="config1/jdbc_config.properties">
<!-- <property name="driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@172.16.1.241:1521:jtcrm" />
<property name="username" value="jtorder" /> <property name="password" value="jtorder"
/> -->
</properties>
<!-- 或者 首先读取properties元素内部的子元素的值, 再读取properties配置文件的值 后者覆盖前者 -->
<!-- <properties resource="config/jdbc.properties"> <property name="driver"
value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@172.16.1.241:1521:jtcrm"
/> </properties> -->
<!-- 设置mybatis3 运行时的行为方式 全局设置 -->
<settings>
<!-- 设置超时时间,它决定驱动等待一个数据库响应的时间 -->
<setting name="defaultStatementTimeout" value="60000" />
<!-- 启用或禁用 缓存 -->
<setting name="cacheEnabled" value="false" />
<!-- 启用或禁用延迟加载。当禁用时, 所有关联对象都会即时加载 -->
<setting name="lazyLoadingEnabled" value="true" />
<!-- 允许或不允许多种结果集从一个单独 的语句中返回(需要适合的驱动) 不用考虑 -->
<setting name="multipleResultSetsEnabled" value="true" />
<!-- 使用列标签代替列名。 不同的驱动在这 方便表现不同 不用考虑 -->
<setting name="useColumnLabel" value="true" />
<!-- 允许 JDBC 支持生成的键。 需要适合的 驱动。 如果设置为 true 则这个设置强制 生成的键被使用 -->
<setting name="useGeneratedKeys" value="false" />
<!-- 等等 -->
</settings>
<!-- 定义类别名,简化xml文件的配置 -->
<typeAliases>
<typeAlias type="com.zxh.customer.bean1.User" alias="User1" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- <property name="driver" value="oracle.jdbc.driver.OracleDriver"
/> <property name="url" value="jdbc:oracle:thin:@172.16.1.241:1521:jtcrm"
/> <property name="username" value="jtorder" /> <property name="password"
value="jtorder" /> -->
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/zxh/customer/bean1/User.xml" />
</mappers>
</configuration>
jdbc_config.properties
driver=oracle.jdbc.driver.OracleDriver
url=jdbc\:oracle\:thin\:@172.16.1.241\:1521\:jtcrm
username=jtorder
password=jtorder
package com.zxh.customer.test1;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.zxh.customer.bean1.User;
import com.zxh.customer.dao1.IUserDao;
import com.zxh.customer.util1.MybatisUtil;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 面向接口编程
* @author zhangxiaohong
*
*/
public class Test {
public static void main(String[] args) {
SqlSession session = MybatisUtil.getSessionFactory().openSession();
try {
IUserDao userDao = session.getMapper(IUserDao.class);
User user = new User();
user.setCustId(611186L);
user.setCustName("中国好样的的了乐乐");
userDao.deleteUser(611186L);
session.commit();
System.out.println(Integer.parseInt("011"));
System.out.println("成功!");
} finally {
session.close();
}
}