参考代码demo1

单个参数

CustomerMapper.java\text {CustomerMapper.java}

1
Customer selectCustomerById(Integer id);

CustomerMapper.xml\text {CustomerMapper.xml}

1
2
3
4
5
6
7
8
9
10
<!--    <select id="selectCustomerById" resultMap="CustomerMap">-->
<!-- SELECT *-->
<!-- FROM customers-->
<!-- WHERE CustomerID = '${id}'-->
<!-- </select>-->
<select id="selectCustomerById" resultMap="CustomerMap">
SELECT *
FROM customers
WHERE CustomerID = #{id}
</select>

测试 \Rightarrow

1
2
3
4
5
6
7
@Test
public void selectCustomerById() {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);
Customer customer = customerMapper.selectCustomerById(1);
System.out.println(customer);
}

多个参数

1⃣arg

CustomerMapper.java\text {CustomerMapper.java}

1
Customer selectCustomerByPostalCodeAndCountry1(String postalCode, String country);

CustomerMapper.xml\text {CustomerMapper.xml}

1
2
3
4
5
6
<select id="selectCustomerByPostalCodeAndCountry1" resultMap="CustomerMap">
SELECT *
FROM customers
WHERE PostalCode = #{arg0}
AND Country = #{arg1}
</select>

测试 \Rightarrow

1
2
3
4
5
6
7
@Test
public void selectCustomerByPostalCodeAndCountry1() {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);
Customer customer = customerMapper.selectCustomerByPostalCodeAndCountry1("98034", "USA");
System.out.println(customer);
}

2⃣param

CustomerMapper.java\text {CustomerMapper.java}

1
Customer selectCustomerByPostalCodeAndCountry1(String postalCode, String country);

CustomerMapper.xml\text {CustomerMapper.xml}

1
2
3
4
5
6
<select id="selectCustomerByPostalCodeAndCountry1" resultMap="CustomerMap">
SELECT *
FROM customers
WHERE PostalCode = #{param1}
AND Country = #{param2}
</select>

测试 \Rightarrow

同上。

3⃣键值对

CustomerMapper.java\text {CustomerMapper.java}

1
Customer selectCustomerByPostalCodeAndCountry2(Map<String, String> postalCodeAndCountry);

CustomerMapper.xml\text {CustomerMapper.xml}

1
2
3
4
5
6
<select id="selectCustomerByPostalCodeAndCountry2" resultMap="CustomerMap">
SELECT *
FROM customers
WHERE PostalCode = #{PostalCode}
AND Country = #{Country}
</select>

测试 \Rightarrow

1
2
3
4
5
6
7
8
9
10
@Test
public void selectCustomerByPostalCodeAndCountry2() {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);
Map<String, String> postalCodeAndCountry = new HashMap<>();
postalCodeAndCountry.put("PostalCode", "98034");
postalCodeAndCountry.put("Country", "USA");
Customer customer = customerMapper.selectCustomerByPostalCodeAndCountry2(postalCodeAndCountry);
System.out.println(customer);
}

4⃣@Param注解

CustomerMapper.java\text {CustomerMapper.java}

1
Customer selectCustomerByPostalCodeAndCountry3(@Param("PostalCode") String postalCode, @Param("Country") String country);

CustomerMapper.xml\text {CustomerMapper.xml}

1
2
3
4
5
6
<select id="selectCustomerByPostalCodeAndCountry3" resultMap="CustomerMap">
SELECT *
FROM customers
WHERE PostalCode = #{PostalCode}
AND Country = #{Country}
</select>

测试 \Rightarrow

1
2
3
4
5
6
7
@Test
public void selectCustomerByPostalCodeAndCountry3() {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);
Customer customer = customerMapper.selectCustomerByPostalCodeAndCountry3("98034", "USA");
System.out.println(customer);
}