文檔版本 | 開發工具 | 測試平臺 | 工程名字 | 日期 | 作者 | 備注 |
---|---|---|---|---|---|---|
V1.0 | 2016.06.28 | lutianfei | none |
整合思路
整合環境
jar包:
工程結構
<!-- 加載配置文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 數據源,使用dbcp -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!-- sqlSessinFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加載mybatis的配置文件 -->
<property name="configLocation" value="mybatis/SqlMapConfig.xml" />
<!-- 數據源 -->
<property name="dataSource" ref="dataSource" />
</bean>
sqlmap/User.xml
在SqlMapconfig.xml中加載User.xml
dao(實現類繼承SqlSessionDaoSupport)
之前dao接口實現類需要注入SqlSessoinFactory,通過spring進行注入。
這里使用spring聲明配置方式,配置dao的bean:
配置dao
<!-- 原始dao接口 -->
<bean id="userDao" class="cn.itcast.ssm.dao.UserDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean
public class UserDaoImplTest {
private ApplicationContext applicationContext;
//在setUp這個方法得到spring容器
@Before
public void setUp() throws Exception {
applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
}
@Test
public void testFindUserById() throws Exception {
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
//調用userDao的方法
User user = userDao.findUserById(1);
System.out.println(user);
}
}
Usermapper.xml和Usermapper.java
通過MapperFactoryBean創建代理對象
此方法問題:
* 這里通過basePackage
屬性配置了mapper的掃描路徑后,在SqlMapperConfig.xml中就不用配置掃描路徑了。
這里使用sqlSessionFactoryBeanName
屬性是由于如果配置的是sqlSessionFactory
屬性,將不會先加載數據庫配置文件及數據源配置(db.properties)
測試代碼
mybaits需要程序員自己編寫sql語句,mybatis官方提供逆向工程 可以針對單表自動生成mybatis履行所需要的代碼(mapper.java,mapper.xml、po..)
企業實際開發中,經常使用的逆向工程方式:由于數據庫的表生成java代碼。
下載逆向工程
建議使用java程序方式,不依賴開發工具。
生成代碼配置文件(有4處需要修改的地方)
<table tableName="items"></table>
<table tableName="orders"></table>
<table tableName="orderdetail"></table>
<table tableName="user"></table>
<?xml version="1.0" encoding="UTF⑻"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是不是去除自動生成的注釋 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--數據庫連接的信息:驅動類、連接地址、用戶名、密碼 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root"
password="mysql">
</jdbcConnection>
<!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg"
userId="yycg"
password="yycg">
</jdbcConnection> -->
<!-- 默許false,把JDBC DECIMAL 和 NUMERIC 類型解析為 Integer,為 true時把JDBC DECIMAL 和
NUMERIC 類型解析為java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成PO類的位置 -->
<javaModelGenerator targetPackage="cn.itcast.ssm.po"
targetProject=".\src">
<!-- enableSubPackages:是不是讓schema作為包的后綴 -->
<property name="enableSubPackages" value="false" />
<!-- 從數據庫返回的值被清算前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映照文件生成的位置 -->
<sqlMapGenerator targetPackage="cn.itcast.ssm.mapper"
targetProject=".\src">
<!-- enableSubPackages:是不是讓schema作為包的后綴 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="cn.itcast.ssm.mapper"
targetProject=".\src">
<!-- enableSubPackages:是不是讓schema作為包的后綴 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定數據庫表 -->
<table tableName="items"></table>
<table tableName="orders"></table>
<table tableName="orderdetail"></table>
<table tableName="user"></table>
</context>
</generatorConfiguration>
履行生成程序
生成后的代碼
需要將生成工程中所生成的代碼拷貝到自己的工程中。
測試ItemsMapper中的方法
//自定義條件查詢
@Test
public void testSelectByExample() {
ItemsExample itemsExample = new ItemsExample();
//通過criteria構造查詢條件
ItemsExample.Criteria criteria = itemsExample.createCriteria();
criteria.andNameEqualTo("筆記本3");
//可能返回多條記錄
List<Items> list = itemsMapper.selectByExample(itemsExample);
System.out.println(list);
}
//根據主鍵查詢
@Test
public void testSelectByPrimaryKey() {
Items items = itemsMapper.selectByPrimaryKey(1);
System.out.println(items);
}
//插入
@Test
public void testInsert() {
//構造 items對象
Items items = new Items();
items.setName("手機");
items.setPrice(999f);
itemsMapper.insert(items);
}
//更新數據
@Test
public void testUpdateByPrimaryKey() {
//對所有字段進行更新,需要先查詢出來再更新
Items items = itemsMapper.selectByPrimaryKey(1);
items.setName("水杯");
itemsMapper.updateByPrimaryKey(items);
//如果傳入字段不空為才更新,在批量更新中使用此方法,不需要先查詢再更新
//itemsMapper.updateByPrimaryKeySelective(record);
}
下一篇 快速冪