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>
- <!-- Configure a built-in transaction manager. If you're using an
- app server, you probably want to use its transaction manager
- and a managed datasource -->
- <properties resource="SqlMap.properties" />
- <transactionManager type="JDBC" commitRequired="false">
- <dataSource type="SIMPLE">
- <property name="JDBC.Driver" value="${driver}"/>
- <property name="JDBC.ConnectionURL" value="${url}"/>
- <property name="JDBC.Username" value="${username}"/>
- <property name="JDBC.Password" value="${password}"/>
- </dataSource>
- </transactionManager>
- <!-- List the SQL Map XML files. They can be loaded from the
- classpath, as they are here (com.domain.data...) -->
- <sqlMap resource="model/Account.xml"/>
- <!-- List more here...
- <sqlMap resource="com/mydomain/data/Order.xml"/>
- <sqlMap resource="com/mydomain/data/Documents.xml"/>
- -->
- </sqlMapConfig>
SqlMap.properties
- driver=com.mysql.jdbc.Driver
- url=jdbc:mysql://localhost:3306/ibatis?useUnicode=true&characterEncoding=UTF-8
- username=root
- password=root
model
- package model;
- public class Account {
- private int id;
- private String firstName;
- private String lastName;
- private String emailAddress;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getFirstName() {
- return firstName;
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public String getLastName() {
- return lastName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- public String getEmailAddress() {
- return emailAddress;
- }
- public void setEmailAddress(String emailAddress) {
- this.emailAddress = emailAddress;
- }
- @Override
- public String toString() {
- return "Account [id=" + id + ", firstName=" + firstName + ", lastName="
- + lastName + ", emailAddress=" + emailAddress + "]";
- }
- }
配置文件Account.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 namespace="Account">
- <!-- Use type aliases to avoid typing the full classname every time. -->
- <typeAlias alias="Account" type="model.Account"/>
- <!-- Result maps describe the mapping between the columns returned
- from a query, and the class properties. A result map isn't
- necessary if the columns (or aliases) match to the properties
- exactly. -->
- <resultMap id="AccountResult" class="Account">
- <result property="id" column="id"/>
- <result property="firstName" column="firstname"/>
- <result property="lastName" column="lastname"/>
- <result property="emailAddress" column="emailaddress"/>
- </resultMap>
- <!-- Select with no parameters using the result map for Account class. -->
- <select id="selectAllAccounts" resultClass="Account">
- select * from ACCOUNT
- </select>
- <!-- A simpler select example without the result map. Note the
- aliases to match the properties of the target result class. -->
- <select id="selectAccountById" parameterClass="int" resultClass="Account">
- select * from ACCOUNT where id = #id#
- </select>
- <!-- Insert example, using the Account parameter class -->
- <insert id="insertAccount" parameterClass="Account">
- insert into ACCOUNT (id,firstname,lastname,emailaddress)
- values (
- #id#, #firstName#, #lastName#, #emailAddress#
- )
- </insert>
- <!-- Update example, using the Account parameter class -->
- <update id="updateAccount" parameterClass="Account">
- update ACCOUNT set
- firstname = #firstName#,
- lastname = #lastName#,
- emailaddress = #emailAddress#
- where
- id = #id#
- </update>
- <!-- Delete example, using an integer as the parameter class -->
- <delete id="deleteAccountById" parameterClass="int">
- delete from ACCOUNT where id = #id#
- </delete>
- <select id="queryAccountByFirstName" parameterClass="String" resultClass="Account">
- select * from account where firstname like '%$firstName$%'
- </select>
- </sqlMap>
dao
- package dao;
- import java.util.List;
- import model.Account;
- public interface AccountDao {
- public void addAccount(Account account);
- public void deleteAccountById(int id);
- public void updateAccount(Account account);
- public List<Account> queryAllAccount();
- public List<Account> queryStudentByFistName(String fistName);
- public Account queryAccountById(int id);
- }
dao.impl
- package 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.sqlmap.client.SqlMapClient;
- import com.ibatis.sqlmap.client.SqlMapClientBuilder;
- import model.Account;
- import dao.AccountDao;
- public class AccountDaoImpl implements AccountDao{
- private static SqlMapClient sqlMapClient = null ;
- static{
- try {
- Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
- sqlMapClient=SqlMapClientBuilder.buildSqlMapClient(reader);
- reader.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- @Override
- public void addAccount(Account account) {
- try {
- this.sqlMapClient.insert("insertAccount", account);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- @Override
- public void deleteAccountById(int id) {
- try {
- this.sqlMapClient.delete("deleteAccountById",id);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- @Override
- public void updateAccount(Account account) {
- // TODO Auto-generated method stub
- try {
- this.sqlMapClient.update("updateAccount", account);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- @Override
- public List<Account> queryAllAccount() {
- // TODO Auto-generated method stub
- List<Account> list = null;
- try {
- list = this.sqlMapClient.queryForList("selectAllAccounts");
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return list;
- }
- @Override
- public List<Account> queryStudentByFistName(String fistName) {
- // TODO Auto-generated method stub
- try {
- return this.sqlMapClient.queryForList("queryAccountByFirstName", fistName);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- }
- @Override
- public Account queryAccountById(int id) {
- Object o =null;
- try {
- o=this.sqlMapClient.queryForList("selectAccountById",id);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return (Account) o;
- }
- public static void main(String[] args) {
- AccountDao accountDao = new AccountDaoImpl();
- /////////////////////////1 queryAllAccount ////////////////////////
- // List<Account> list = accountDao.queryAllAccount();
- // for(Account a:list){
- // System.out.println(a);
- // }
- ////////////////////////2 queryAccount///////////////////////
- // Account a = accountDao.queryAccountById(1);
- // System.out.println(a);
- ////////////////////////3 addAccount//////////////////////////
- // Account a = new Account();
- // a.setEmailAddress("soukenan@qq.com");
- // a.setFirstName("柯南");
- // accountDao.addAccount(a);
- ////////////////////////4 deleteAccountById////////////////////////////////
- // accountDao.deleteAccountById(2);
- ////////////////////////5 ////////////////////////////////////////
- // Account a = new Account();
- // a.setId(1);
- // a.setFirstName("update");
- // accountDao.updateAccount(a);
- ////////////////////////6//////////////////////
- List<Account> l = accountDao.queryStudentByFistName("");
- for(Account o : l){
- System.out.println(o);
- }
- }
- }