1 項目說明java
項目採用 maven 組織 ,依賴 mysql-connector-java,org.mybatis,junit pom 依賴以下:mysql
mysql 數據鏈接 :sql
mysql-connector-javaapache
mybatis session
mybatismybatis
junitapp
junit 單元測試,本項目採用junit 跑單元測試,4.12 版本採用 @Test註解 測試方法,3.X 版本採用繼承來實現測試方法maven
<dependencies> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.30</version> </dependency> </dependencies>
2 項目結構ide
3 DB 腳本單元測試
DROP TABLE IF EXISTS `account`; CREATE TABLE `account` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(45) DEFAULT NULL, `money` decimal(10,0) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=103 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of account -- ---------------------------- INSERT INTO `account` VALUES ('1', 'hbb0b0', '40000'); INSERT INTO `account` VALUES ('2', 'kael', '1000');
4 代碼說明
MyDataSourceFactory
package hbb0b0.JavaBasic.mybatis03; import java.util.Properties; import javax.sql.DataSource; import org.apache.ibatis.datasource.DataSourceFactory; import org.apache.ibatis.datasource.pooled.PooledDataSource; public class MyDataSourceFactory implements DataSourceFactory { private Properties prop; public DataSource getDataSource() { PooledDataSource ds = new PooledDataSource(); ds.setDriver(prop.getProperty("driver")); ds.setUrl(prop.getProperty("url")); ds.setUsername(prop.getProperty("user")); ds.setPassword(prop.getProperty("password")); return ds; } public void setProperties(Properties prprts) { prop = prprts; } }
package hbb0b0.JavaBasic.model; import java.io.Serializable; public class Account implements Serializable{ private int id; private String name; private double money; public Account() { super(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } @Override public String toString() { return "Account [id=" + id + ", name=" + name + ", money=" + money + "]"; } }
package hbb0b0.JavaBasic.mybatis03; import org.apache.ibatis.annotations.Select; public interface AccountMapper { @Select("SELECT * FROM Account WHERE id = #{id}") Account getAccountById(int id); }
package hbb0b0.JavaBasic.mybatis03; import java.util.Properties; import javax.sql.DataSource; import org.apache.ibatis.mapping.Environment; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.apache.ibatis.transaction.TransactionFactory; import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; public class DbTool { private static SqlSessionFactory m_sessionFactory; private static void initSessionFactory() { Properties prop = new Properties(); prop.setProperty("driver", "com.mysql.jdbc.Driver"); prop.setProperty("url", "jdbc:mysql://localhost:3306/Study"); prop.setProperty("user", "root"); prop.setProperty("password", "sqlsa"); MyDataSourceFactory mdsf = new MyDataSourceFactory(); mdsf.setProperties(prop); DataSource ds = mdsf.getDataSource(); TransactionFactory trFact = new JdbcTransactionFactory(); Environment environment = new Environment("development", trFact, ds); Configuration config = new Configuration(environment); config.addMapper(AccountMapper.class); m_sessionFactory = new SqlSessionFactoryBuilder().build(config); } public static SqlSessionFactory GetSqlSessionFactory() { if(m_sessionFactory==null) { initSessionFactory(); } return m_sessionFactory; } }
package hbb0b0.JavaBasic.mybatis03; import static org.junit.Assert.assertNotNull; import java.io.IOException; import org.apache.ibatis.session.SqlSession; import org.junit.Assert; import org.junit.Test; import java.lang.System; /** * Unit test for simple App. */ public class AppTest { @Test public void getOne_Test() throws IOException { SqlSession session = DbTool.GetSqlSessionFactory().openSession(); try { AccountMapper mapper= session.getMapper(AccountMapper.class); assertNotNull(mapper); Account account = mapper.getAccountById(1); assertNotNull(account); Assert.assertEquals("hbb0b0",account.getName()); System.out.println(account.toString()); } finally { session.close(); } } }
測試結果: