因爲咱們的所編寫的dao層、service代碼可能前臺和後臺都須要用到的,所以咱們把環境搭建在core模塊中css
首先,咱們要作的就是品牌管理,咱們首先來看看原型界面的的品牌管理java
涉及到的數據庫表在咱們的powerdesigner中就已經定義過了mysql
咱們來看一下對應數據庫表的數據web
<build>
<finalName>zhongfucheng</finalName>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
<!-- 制定mysql的驅動包的路徑 千萬別放中文路徑下 -->
<classPathEntry location="F:\傳智168期JAVA\傳智168期JAVA\移動商城\shoprepository\shoprepository\repository\com\oracle\ojdbc14\10.2.0.2.0\ojdbc14-10.2.0.2.0.jar" />
<!-- 配置數據源和生成的代碼所存放的位置 -->
<context id="context1">
<commentGenerator>
<!-- 是否取消註釋 -->
<property name="suppressAllComments" value="true" />
<!-- 是否生成註釋代時間戳 -->
<property name="suppressDate" value="true" />
</commentGenerator>
<jdbcConnection driverClass="oracle.jdbc.OracleDriver" connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:ZHONGFUCHENG" userId="zhongfucheng" password="zhong" />
<!-- 所生成的實體類的位置默認資源包src -->
<javaModelGenerator targetPackage="com.rl.ecps.model" targetProject="src/main/java" />
<!-- 所生成的sqlMap的影射文件的位置,默認資源包src -->
<sqlMapGenerator targetPackage="com.rl.ecps.sqlMap" targetProject="src/main/java" />
<!-- 爲哪些表生成代碼 tableName:表名 schema:不用填寫 -->
<table schema="" tableName="eb_brand" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" >
</table>
</context>
</generatorConfiguration>
生成的文件:spring
sqlMapping映射文件也算是一種資源文件把,所以我把它移到resources中了sql
sqlMapConfig.xml數據庫
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<mappers>
<mapper resource="sqlMap/EbBrandMapper.xml"/>
</mappers>
</configuration>
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--開啓註解--> <context:component-scan base-package="com.rl.ecps"/> <!--配置鏈接池--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.OracleDriver"></property> <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:ZHONGFUCHENG"></property> <property name="username" value="zhongfucheng"></property> <property name="password" value="zhong"></property> </bean> <!--配置sessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:sqlMapConfig.xml"></property> </bean> <!--配置事務管理器--> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--配置事務具體方法--> <tx:advice id="txAdive" transaction-manager="txManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="modify*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="remove*" propagation="REQUIRED"/> <tx:method name="select*" read-only="true"/> <tx:method name="get*" read-only="true"/> </tx:attributes> </tx:advice> <!--配置切面--> <aop:config> <aop:advisor advice-ref="txAdive" pointcut="execution(* com.rl.ecps.service..*.*(..))"/> </aop:config> </beans>
log4j.propertiesapache
log4j.rootLogger=DEBUG, Console
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
interface EbBrandDao {
void saveBrand(EbBrand brand);
EbBrand getBrandById(Long brandId);
void updateBrand(EbBrand brand);
void deleteBrand(Long brandId);
List<EbBrand> selectBrand();
List<EbBrand> selectBrandByName(String brandName);
}
/** * 繼承SqlSessionDaoSupport可以獲得sessionFactory的引用,很是方便! */
@Repository
public class EbBrandDaoImpl extends SqlSessionDaoSupport implements EbBrandDao {
String nameSpace = "com.rl.ecps.sqlMap.EbBrandMapper.";
public void saveBrand(EbBrand brand) {
this.getSqlSession().insert(nameSpace + "insert", brand);
}
public EbBrand getBrandById(Long brandId) {
return this.getSqlSession().selectOne(nameSpace + "selectByPrimaryKey", brandId);
}
public void updateBrand(EbBrand brand) {
/** * updateByPrimaryKeySelective和updata的區別: * 一個是動態SQL,一個是靜態SQL。這裏就使用動態SQL比較好。由於是更新操做 */
this.getSqlSession().update(nameSpace + "updateByPrimaryKeySelective", brand);
}
public void deleteBrand(Long brandId) {
this.getSqlSession().delete(nameSpace + "deleteByPrimaryKey", brandId);
}
public List<EbBrand> selectBrand() {
return this.getSqlSession().selectList(nameSpace+"selectBrand");
}
public List<EbBrand> selectBrandByName(String brandName) {
return this.getSqlSession().selectList(nameSpace+"selectBrandByName", brandName);
}
}
mybatis插件只有最後兩個方法沒有幫咱們自動生成,其餘的SQL語句都自動生成了。markdown
<select id="selectBrand" resultMap="BaseResultMap">
select * from eb_brand
</select>
<select id="selectBrandByName" resultMap="BaseResultMap" parameterType="string">
select * from eb_brand t where t.brand_name = #{brandName}
</select>
public interface EbBrandService {
void saveBrand(EbBrand brand);
EbBrand getBrandById(Long brandId);
void updateBrand(EbBrand brand);
void deleteBrand(Long brandId);
List<EbBrand> selectBrand();
List<EbBrand> selectBrandByName(String brandName);
}
package com.rl.ecps.service.impl;
import com.rl.ecps.dao.EbBrandDao;
import com.rl.ecps.dao.impl.EbBrandDaoImpl;
import com.rl.ecps.model.EbBrand;
import com.rl.ecps.service.EbBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/** * Created by ozc on 2017/8/26. */
@Service
public class EbBrandServiceImpl implements EbBrandService {
@Autowired
private EbBrandDao brandDao;
public void saveBrand(EbBrand brand) {
brandDao.saveBrand(brand);
}
public EbBrand getBrandById(Long brandId) {
return brandDao.getBrandById(brandId);
}
public void updateBrand(EbBrand brand) {
brandDao.updateBrand(brand);
}
public void deleteBrand(Long brandId) {
brandDao.deleteBrand(brandId);
}
public List<EbBrand> selectBrand() {
return brandDao.selectBrand();
}
public List<EbBrand> selectBrandByName(String brandName) {
return brandDao.selectBrandByName(brandName);
}
}
在idea中,咱們新建一個test目錄session
對着咱們的serviceImpl使用快捷鍵ctrl+shift+t
idea就會幫咱們自動把想要測試的方法把相關的目錄建立出來了
咱們在插入數據的時候,記得要改寫SQL語句,由於默認是沒有主鍵的。下面就是咱們把在主鍵列上使用自動增加
<insert id="insert" parameterType="com.rl.ecps.model.EbBrand" >
<selectKey keyProperty="brandId" order="BEFORE" resultType="long">
select seqbrandid.nextval from dual
</selectKey>
insert into EB_BRAND (BRAND_ID, BRAND_NAME, BRAND_DESC,
IMGS, WEBSITE, BRAND_SORT
)
values (#{brandId,jdbcType=DECIMAL}, #{brandName,jdbcType=VARCHAR}, #{brandDesc,jdbcType=VARCHAR},
#{imgs,jdbcType=VARCHAR}, #{website,jdbcType=VARCHAR}, #{brandSort,jdbcType=DECIMAL}
)
</insert>
插入成功