[sql] view plain copy

  [sql] view plain copy
  
  CREATE TABLE SYS_USER (
  
  USER_CODE VARCHAR( 36 ) NOT NULL,
  
  LOGIN_NAME VARCHAR( 50 ) NOT NULL,
  
  LOGIN_PWD VARCHAR( 50 ) NOT NULL,
  
  USER_LEVEL TINYINT NOT NULL
  
  );
  
  並執行一條測試數據:
  
  [sql] view plain copy
  
  INSERT INTO `sys_user` (`USER_CODE`, `LOGIN_NAME`, `LOGIN_PWD`, `USER_LEVEL`) VALUES ('1', 'ADMIN', 'ADMIN', '1')
  
  二、修改pom.xml
  
  增長阿里 druid 鏈接池及 mysql:
  
  [html] view plain copy
  
  <dependency>
  
  <groupId>com.alibaba</groupId>
  
  <artifactId>druid</artifactId>
  
  <version>1.0.31</version>
  
  </dependency>
  
  <dependency>
  
  <groupId>mysql</groupId>
  
  <artifactId>mysql-connector-java</artifactId>
  
  <version>5.1.42</version>
  
  </dependency>
  
  增長 mybatis:
  
  [html] view plain copy
  
  <dependency>
  
  <groupId>org.mybatis</groupId>
  
  <artifactId>mybatis</artifactId>
  
  <version>3.4.4</version>
  
  </dependency>
  
  <dependency>
  
  <groupId>org.mybatis</groupId>
  
  <artifactId>mybatis-spring</artifactId>
  
  <version>1.3.1</version>
  
  </dependency>
  
  增長 spring aop 依賴(非必需):
  
  [html] view plain copy
  
  <dependency>
  
  <groupId>org.aspectj</groupId>
  
  <artifactId>aspectjrt</artifactId>
  
  <version>1.6.11</version>
  
  </dependency>
  
  <dependency>
  
  <groupId>org.aspectj</groupId>
  
  <artifactId>aspectjweaver</artifactId>
  
  <version>1.6.11</version>
  
  </dependency>
  
  最後完成的pom.xml內容以下:
  
  [html] view plain copy
  
  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  
  <modelVersion>4.0.0</modelVersion>
  
  <groupId>com.whowii</groupId>
  
  <artifactId>website_java3</artifactId>
  
  <packaging>war</packaging>
  
  <version>0.0.1-SNAPSHOT</version>
  
  <name>website_java3 Maven Webapp</name>
  
  <url>http://maven.apache.org</url>
  
  <dependencies>
  
  <dependency>
  
  <groupId>junit</groupId>
  
  <artifactId>junit</artifactId>
  
  <version>3.8.1</version>
  
  <scope>test</scope>
  
  </dependency>
  
  <!-- database -->
  
  <dependency>
  
  <groupId>com.alibaba</groupId>
  
  <artifactId>druid</artifactId>
  
  <version>1.0.31</version>
  
  </dependency>
  
  <dependency>
  
  <groupId>mysql</groupId>
  
  <artifactId>mysql-connector-java</artifactId>
  
  <version>5.1.42</version>
  
  </dependency>
  
  <!-- mybatis -->
  
  <dependency>
  
  <groupId>org.mybatis</groupId>
  
  <artifactId>mybatis</artifactId>
  
  <version>3.4.4</version>
  
  </dependency>
  
  <dependency>
  
  <groupId>org.mybatis</groupId>
  
  <artifactId>mybatis-spring</artifactId>
  
  <version>1.3.1</version>
  
  </dependency>
  
  <!-- spring -->
  
  <dependency>
  
  <groupId>org.springframework</groupId>
  
  <artifactId>spring-context</artifactId>
  
  <version>$www.sb45475.com{springVersion}</version>
  
  </dependency>
  
  <dependency>
  
  <groupId>org.springframework</groupId>
  
  <artifactId>spring-webmvc</artifactId>
  
  <version>${springVersion}</version>
  
  </dependency>
  
  <dependency>
  
  <groupId>org.springframework</groupId>
  
  <artifactId>spring-beans</artifactId>
  
  <version>${springVersion}</version>
  
  <type>jar</type>
  
  </dependency>
  
  <dependency>
  
  <groupId>org.springframework</groupId>
  
  <artifactId>spring-orm</artifactId>
  
  <version>$www.yigouylpt2.com{springVersion}</version>
  
  <type>jar</type>
  
  </dependency>
  
  <dependency>
  
  <groupId>org.aspectj</groupId>
  
  <artifactId>aspectjrt</artifactId>
  
  <version>1.6.11</version>
  
  </dependency>
  
  <dependency>
  
  <groupId>org.aspectj<www.yuheng119.com /groupId>
  
  <artifactId>aspectjweaver</artifactId>
  
  <version>1.6.11</version>
  
  </dependency>
  
  <!-- log4j -->
  
  <dependency>
  
  <groupId>org.slf4j</groupId>
  
  <artifactId>slf4j-api</artifactId>
  
  <version>1.6.6</version>
  
  </dependency>
  
  <dependency>
  
  <groupId>org.slf4j</groupId>
  
  <artifactId>slf4j-log4j12</artifactId>
  
  <version>1.6.6</version>
  
  </dependency>
  
  <dependency>
  
  <groupId>log4j</groupId>
  
  <artifactId>log4j</artifactId>
  
  <version>1.2.16www.huazongyule.com/</version>
  
  </dependency>
  
  </dependencies>
  
  <build>
  
  <finalName>website_java3</finalName>
  
  </build>
  
  <properties>
  
  <springVersion>4.3.8.RELEASE</springVersion>
  
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  
  </properties>
  
  </project>
  
  原本是想使用 sqlite 的,但在實際使用時遇到不少問題,主要是druid對sqlite支持不夠好。
  
  三、增長 source.properties
  
  在 src/main/www.acnet.cn/ resources 下添加 source.properties 配置文件,內容以下:
  
  [plain] view plain copy
  
  # --------------------數據源配置----------------------
  
  # sqlite
  
  #jdbc.type=sqlite
  
  #jdbc.driver=org.sqlite.JDBC
  
  #jdbc.url=jdbc:sqlite:d:/website_java3.s3db
  
  #jdbc.username=
  
  #jdbc.password=
  
  # Mysql
  
  jdbc.type=mysql
  
  jdbc.driver=com.mysql.jdbc.Driver
  
  jdbc.url=jdbc:mysql://localhost:3306/website_java3?characterEncoding=utf8&useSSL=true
  
  jdbc.username=root
  
  jdbc.password=root
  
  # Oracle
  
  #jdbc.type=oracle
  
  #jdbc.driver=oracle.jdbc.driver.OracleDriver
  
  #jdbc.url=jdbc:oracle:thin:@192.168.1.10:1521:orcl
  
  #jdbc.username=website_java3
  
  #jdbc.password=website_java3
  
  #druid datasource
  
  druid.initialSize=10
  
  druid.minIdle=10
  
  druid.maxActive=50
  
  druid.maxWait=60000
  
  druid.timeBetweenEvictionRunsMillis=30000
  
  druid.minEvictableIdleTimeMillis=150000
  
  druid.validationQuery=SELECT 'x'
  
  druid.validationQueryTimeout=3600
  
  druid.testWhileIdle=true
  
  druid.testOnBorrow=false
  
  druid.testOnReturn=false
  
  druid.removeAbandoned=true
  
  druid.removeAbandonedTimeout=1800000
  
  druid.logAbandoned=true
  
  druid.poolPreparedStatements=true
  
  druid.maxPoolPreparedStatementPerConnectionSize=20
  
  druid.filters=wall,stat
  
  這裏只使用了 mysql 數據庫配置。
  
  四、增長 mybatis 配置
  
  在 src/main/resources 下添加 mybatis-config.xml 配置文件,內容以下:
  
  [html] view plain copy
  
  <?xml version="1.0" encoding="UTF-8" ?>
  
  <!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
  
  <configuration>
  
  <!--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  
  mybatis 擴展配置
  
  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
  
  <!-- 全局參數 -->
  
  <settings>
  
  <!-- 使全局的映射器啓用或禁用緩存。 -->
  
  <setting name="cacheEnabled" value="true"/>
  
  <!-- 全局啓用或禁用延遲加載。當禁用時,全部關聯對象都會即時加載。 -->
  
  <setting name="lazyLoadingEnabled" value="true"/>
  
  <!-- 當啓用時,有延遲加載屬性的對象在被調用時將會徹底加載任意屬性。不然,每種屬性將會按須要加載。 -->
  
  <setting name="aggressiveLazyLoading" value="true"/>
  
  <!-- 是否容許單條sql 返回多個數據集 (取決於驅動的兼容性) default:true -->
  
  <setting name="multipleResultSetsEnabled" value="true"/>
  
  <!-- 是否可使用列的別名 (取決於驅動的兼容性) default:true -->
  
  <setting name="useColumnLabel" value="true"/>
  
  <!-- 容許JDBC 生成主鍵。須要驅動器支持。若是設爲了true,這個設置將強制使用被生成的主鍵,有一些驅動器不兼容不過仍然能夠執行。 default:false -->
  
  <setting name="useGeneratedKeys" value="false"/>
  
  <!-- 指定 MyBatis 如何自動映射 數據基表的列 NONE:不隱射 PARTIAL:部分 FULL:所有 -->
  
  <setting name="autoMappingBehavior" value="PARTIAL"/>
  
  <!-- 這是默認的執行類型 (SIMPLE: 簡單; REUSE: 執行器可能重複使用prepared statements語句;BATCH: 執行器能夠重複執行語句和批量更新) -->
  
  <setting name="defaultExecutorType" value="SIMPLE"/>
  
  <!-- 使用駝峯命名法轉換字段。 -->
  
  <setting name="mapUnderscoreToCamelCase" value="true"/>
  
  <!-- 設置本地緩存範圍 session:就會有數據的共享 statement:語句範圍 (這樣就不會有數據的共享 ) defalut:session -->
  
  <setting name="localCacheScope" value="SESSION"/>
  
  <!-- 設置但JDBC類型爲空時,某些驅動程序 要指定值,default:OTHER,插入空值時不須要指定類型 -->
  
  <setting name="jdbcTypeForNull" value="NULL"/>
  
  </settings>
  
  </configuration>
  
  五、修改 spring 配置
  
  修改 spring-context.xml 文件,增長數據源、session 工廠、事務等配置,完成後的內容以下:
  
  [html] view plain copy
  
  <?xml version="1.0" encoding="UTF-8"?>
  
  <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  
  xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
  
  xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  
  xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
  
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  
  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  
  http://www.springframework.org/schema/aop
  
  http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  
  http://www.springframework.org/schema/context
  
  http://www.springframework.org/schema/context/spring-context-4.0.xsd
  
  http://www.springframework.org/schema/tx
  
  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  
  http://www.springframework.org/schema/cache
  
  http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
  
  <!-- 數據源:加載數據庫配置文件 -->
  
  <bean id="propertyConfigurer"
  
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  
  <property name="location">
  
  <value>classpath:source.properties</value>
  
  </property>
  
  <property name="fileEncoding" value="utf-8" />
  
  </bean>
  
  <!-- 數據源:配置參數 -->
  
  <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  
  <!-- 數據源驅動類可不寫,Druid默認會自動根據URL識別DriverClass -->
  
  <property name="driverClassName" value="${jdbc.driver}" />
  
  <!-- 基本屬性 url、user、password -->
  
  <property name="url" value="${jdbc.url}"/>
  
  <property name="username" value="${jdbc.username}"/>
  
  <property name="password" value="${jdbc.password}"/>
  
  <property name="dbType" value = "mysql" />
  
  <!-- 配置初始化大小、最小、最大 -->
  
  <property name="initialSize" value="${druid.initialSize}"/><!-- 初始化鏈接數量 -->
  
  <property name="minIdle" value="${druid.minIdle}"/><!-- 最小空閒鏈接數量 -->
  
  <property name="maxActive" value="${druid.maxActive}"/><!-- 最大鏈接數量 -->
  
  <!-- 配置獲取鏈接等待超時的時間 -->
  
  <property name="maxWait" value="${druid.maxWait}"/><!-- 最大創建鏈接等待時間(毫秒)。若是超過此時間將接到異常。設爲-1表示無限制-->
  
  <!-- 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 -->
  
  <property name="timeBetweenEvictionRunsMillis" value="${druid.timeBetweenEvictionRunsMillis}" />
  
  <!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 -->
  
  <property name="minEvictableIdleTimeMillis" value="${druid.minEvictableIdleTimeMillis}" />
  
  <property name="validationQuery" value="${druid.validationQuery}" />
  
  <property name="validationQueryTimeout" value="${druid.validationQueryTimeout}" />
  
  <property name="testWhileIdle" value="${druid.testWhileIdle}" /><!--空閒時是否進行驗證,檢查對象是否有效 -->
  
  <property name="testOnBorrow" value="${druid.testOnBorrow}" /><!--取得對象時是否進行驗證,檢查對象是否有效 -->
  
  <property name="testOnReturn" value="${druid.testOnReturn}" /><!--返回對象時是否進行驗證 -->
  
  <property name="removeAbandoned" value="${druid.removeAbandoned}" />
  
  <property name="removeAbandonedTimeout" value="${druid.removeAbandonedTimeout}" />
  
  <property name="logAbandoned" value="${druid.logAbandoned}" />
  
  <!-- 打開PSCache,而且指定每一個鏈接上PSCache的大小 oracle=true mysql=false -->
  
  <property name="poolPreparedStatements" value="${druid.poolPreparedStatements}" /><!-- 代表是否開啓statement cache,默認爲false,也就是不開啓 -->
  
  <property name="maxPoolPreparedStatementPerConnectionSize" value="${druid.maxPoolPreparedStatementPerConnectionSize}" /><!-- statement cache的大小,默認爲-1,也就是不限制 -->
  
  <!-- 配置監控統計攔截的filters -->
  
  <property name="filters" value="${druid.filters}" /><!-- 配置監控統計攔截的filters,去掉後監控界面sql沒法統計 -->
  
  </bean>
  
  <!-- session 工廠 -->
  
  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  
  <!-- 用於鏈接數據庫的數據源 -->
  
  <property name="dataSource" ref="dataSource" />
  
  <!-- 指定Mybatis的配置文件位置 -->
  
  <property name="configLocation" value="classpath:mybatis-config.xml" />
  
  <!-- 註冊類(bean)的別名,該屬性能夠配置多個,能夠用,;\t\n進行分割,不支持ant通配符 -->
  
  <property name="typeAliasesPackage" value="com.whowii.core.manage.model"/>
  
  <!-- 配置映射文件,該路徑支持多個,能夠用,;\t\n進行分割,支持ant通配符 -->
  
  <property name="mapperLocations">
  
  <array>
  
  <value>classpath:com/whowii/core/manage/mapper/sysuser-mapper.xml</value>
  
  </array>
  
  </property>
  
  </bean>
  
  <!--++++++++++++++++++++++++++++++++ 事務配置 +++++++++++++++++++++++++++++++-->
  
  <!-- 事務:聲明管理器 -->
  
  <bean id="transactionManager"
  
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  
  <property name="dataSource" ref="dataSource" />
  
  </bean>
  
  <tx:annotation-driven transaction-manager="transactionManager"/>
  
  <!-- 數據層配置,自動掃描,由 MapperScannerConfigurer自動生成 mapper bean -->
  
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  
  <property name="basePackage" value="com.whowii.core.manage.dao" />
  
  </bean>
  
  <!-- 自動掃描業務包 -->
  
  <context:component-scan base-package="com.whowii.core.manage.service.impl" />
  
  </beans>
  
  六、添加 model
  
  添加包「com.whowii.core.manage.model」,並添加實體類 SysUser,代碼以下;
  
  [java] view plain copy
  
  package com.whowii.core.manage.model;
  
  public class SysUser implements java.io.Serializable {
  
  private static final long serialVersionUID = 7442514800601880744L;
  
  private String userCode;
  
  private String loginName;
  
  private String loginPwd;
  
  private Byte userLevel;
  
  public String getUserCode() {
  
  return userCode;
  
  }
  
  public void setUserCode(String userCode) {
  
  this.userCode = userCode;
  
  }
  
  public String getLoginName() {
  
  return loginName;
  
  }
  
  public void setLoginName(String loginName) {
  
  this.loginName = loginName;
  
  }
  
  public String getLoginPwd() {
  
  return loginPwd;
  
  }
  
  public void setLoginPwd(String loginPwd) {
  
  this.loginPwd = loginPwd;
  
  }
  
  public Byte getUserLevel() {
  
  return userLevel;
  
  }
  
  public void setUserLevel(Byte userLevel) {
  
  this.userLevel = userLevel;
  
  }
  
  }
  
  七、添加 dao
  
  添加包「com.whowii.core.manage.dao」,並添加接口 SysUserDao,代碼以下:
  
  [java] view plain copy
  
  package com.whowii.core.manage.dao;
  
  import com.whowii.core.manage.model.SysUser;
  
  public interface SysUserDao {
  
  /**
  
  * 由用戶編碼獲取用戶
  
  *
  
  * @param userCode
  
  * @return
  
  */
  
  public SysUser get(String userCode);
  
  }
  
  八、添加 mapper
  
  添加包「com.whowii.core.manage.mapper」,並添加映射文件「sysuser-mapper.xml」,內容以下:
  
  [html] view plain copy
  
  <?xml version="1.0" encoding="UTF-8" ?>
  
  <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
  
  "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
  
  <mapper namespace="com.whowii.core.manage.dao.SysUserDao">
  
  <!-- 定義的resultMap,以解決類的屬性名和數據庫列名不一致的問題
  
  其中的type是mybatis配置文件中定義的別名
  
  -->
  
  <resultMap id="result" type="SysUser">
  
  <id column="USER_CODE" property="userCode" />
  
  <result column="LOGIN_NAME" property="loginName" />
  
  <result column="LOGIN_PWD" property="loginPwd" />
  
  <result column="USER_LEVEL" property="userLevel" />
  
  </resultMap>
  
  <!-- 獲取一個用戶 -->
  
  <select id="get" parameterType="string" resultMap="result" >
  
  SELECT USER_CODE,LOGIN_NAME,LOGIN_PWD,USER_LEVEL FROM SYS_USER WHERE USER_CODE = #{userCode}
  
  </select>
  
  </mapper>
  
  九、修改服務層代碼
  
  修改 DemoService 接口和 DemoServiceImpl 實現類,分別以下:
  
  [java] view plain copy
  
  package com.whowii.core.manage.service;
  
  public interface DemoService {
  
  public String getUser(String userCode);
  
  }
  
  [java] view plain copy
  
  package com.whowii.core.manage.service.impl;
  
  import org.springframework.beans.factory.annotation.Autowired;
  
  import org.springframework.stereotype.Service;
  
  import org.springframework.transaction.annotation.Transactional;
  
  import com.whowii.core.manage.dao.SysUserDao;
  
  import com.whowii.core.manage.model.SysUser;
  
  import com.whowii.core.manage.service.DemoService;
  
  @Service("demoService")
  
  public class DemoServiceImpl implements DemoService {
  
  @Autowired
  
  private SysUserDao userDao;
  
  @Override
  
  @Transactional
  
  public String getUser(String userCode) {
  
  SysUser u = this.userDao.get(userCode);
  
  return u.getLoginName();
  
  }
  
  }
  
  十、修改控制器
  
  修改 DemoController ,完成後的代碼以下:
  
  [java] view plain copy
  
  package com.whowii.core.manage.controller;
  
  import javax.annotation.Resource;
  
  import org.springframework.stereotype.Controller;
  
  import org.springframework.web.bind.annotation.RequestMapping;
  
  import com.whowii.core.manage.service.DemoService;
  
  @Controller
  
  public class DemoController {
  
  // private Logger logger = Logger.getLogger(getClass());
  
  private DemoService demoService;
  
  @Resource(name = "demoService")
  
  public void setDemoService(DemoService demoService) {
  
  this.demoService = demoService;
  
  }
  
  @RequestMapping("/index")
  
  public String index() {
  
  String s = this.demoService.getUser("1");
  
  System.out.println(">>=======================");
  
  System.out.println(">>" + s);
  
  return "demo";
  
  }
  
  }html

相關文章
相關標籤/搜索