1.输入映射
输入映射支持的类型:
1) 基本的类型,int,String,double 等(*)
2) JavaBean 类型(*)
3) 包装JavaBean 类型(对象里面包含另一个对象)
1.1基本类型
<insert id="testParameterType" parameterType="String">
INSERT INTO t_customer(NAME) VALUES(#{name})
</insert>
#{name}也可以自定义,如#{value}。
注意:使用单一参数时可这样定义,多个参数时不可以,需要与定义的名称一致。
// 输入映射 public void testParameterType(String name);
/**
* 输入映射-基本类型
*/
@Test
public void test1() {
SqlSession sqlSession = SessionUtils.getSession();
// getMapper(): 返回指定接口的动态代理的实现类对象
CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
dao.testParameterType("张三");
sqlSession.commit();
sqlSession.close();
}
1.2JavaBean 类型
<insert id="testParameterType" parameterType="Customer">
INSERT INTO t_customer(NAME,gender,telephone) VALUES(#{name},#{gender},#{telephone})
</insert>
public void testParameterType(Customer c);
/**
* 输入映射-Javabean
*/
@Test
public void test1() {
SqlSession sqlSession = SessionUtils.getSession();
// getMapper(): 返回指定接口的动态代理的实现类对象
CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
Customer c = new Customer();
c.setName("张三666");
c.setGender("男");
c.setTelephone("123456789");
dao.testParameterType(c);
sqlSession.commit();
sqlSession.close();
}
1.3包装JavaBean 类型(对象里面包含另一个对象)
一个对象里面包含另一个对象
CustomerVo.java:
package cn.sm1234.domain; /**
* CustomerVo包装JaveBean类型
* @author Jack
*
*/
public class CustomerVo { private Customer customer; public Customer getCustomer() {
return customer;
} public void setCustomer(Customer customer) {
this.customer = customer;
}
}
<insert id="testParameterType" parameterType="CustomerVo">
<!--
customer.name传值说明:
customer来源于CustomerVo.java中的private Customer customer;定义
-->
INSERT INTO t_customer(NAME,gender,telephone) VALUES(#{customer.name},#{customer.gender},#{customer.telephone})
</insert>
public void testParameterType(CustomerVo c);
/**
* 输入映射-包装JavaBean 类型(对象里面包含另一个对象)
*/
@Test
public void test1() {
SqlSession sqlSession = SessionUtils.getSession();
// getMapper(): 返回指定接口的动态代理的实现类对象
CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
CustomerVo vo = new CustomerVo();
Customer c = new Customer();
c.setName("张三333");
c.setGender("男");
c.setTelephone("123456789");
vo.setCustomer(c);
dao.testParameterType(vo);
sqlSession.commit();
sqlSession.close();
}
补充:sqlMapConfig.xml定义别名方式
<!-- 定义别名 -->
<typeAliases>
<!-- type: 需要映射的类型 alias: 别名 -->
<!-- <typeAlias type="cn.sm1234.domain.Customer" alias="customer"/> -->
<!-- ibatis3.0推荐使用的标签,定位到包名即可 -->
<package name="cn.sm1234.domain" />
</typeAliases>