第一步创建动态web工程WebService
第二部导入jar包,设置BuildPath
第三步在src下新建resource包,新建db.properties文件,存放数据库链接信息
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ebiztest
user=root
password=123
第四步新建数据传输对象包dto,和UserDTO实体类
package dto;
public class UserDTO {
/**
* 用户名
*/
public String username;
/**
* 密码
*/
public String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
第五步resource包新建UserMapper.xml存放sql查询语句
<?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="UserDTO" type="dto.UserDTO" />
<select id="getUser" resultClass="UserDTO">
select username,password from userformvcibatis where id=1
</select>
</sqlMap>
第六步resource包下新建SqlMapConfig.xml文件,考虑到与spring的整合,将链接数据库的部分转移到spring容器的配置文件中,所以这里仅仅链接存放sql语句的映射文件UserMapper.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>
<sqlMap resource="resource/UserMapper.xml" />
</sqlMapConfig>
第七步resource包下新建root-context.xml文件,spring容器的配置文件,扫描业务层,配置链接数据库的相关信息和用到的bean,注意spring对ibatis的sqlmapclient bean 做了进一步的封装,封装为sqlMapClientTemplate模板类
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/