关于vue.js:ssm框架应该如何操作

67次阅读

共计 6241 个字符,预计需要花费 16 分钟才能阅读完成。

导入开发包
asm-3.2.0.RELEASE.jar
asm-3.3.1.jar
c3p0-0.9.jar
cglib-2.2.2.jar
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
jackson-core-asl-1.7.2.jar
jackson-mapper-asl-1.7.2.jar
javassist-3.17.1-GA.jar
javax.servlet.jsp.jstl.jar
jsf-api.jar
jsf-impl.jar
jstl-impl.jar
junit.jar
log4j-1.2.17.jar
mybatis-3.2.2.jar
mybatis-spring-1.2.0.jar
mysql-connector-java-5.1.26-bin.jar
org.hamcrest.core_1.1.0.v20090501071000.jar
org.springframework.transaction-3.2.2.RELEASE.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
spring-aop-3.2.0.RELEASE.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
spring-jdbc-3.2.0.RELEASE.jar
spring-orm-3.2.0.RELEASE.jar
spring-test-3.2.0.RELEASE.jar
spring-web-3.2.0.RELEASE.jar
spring-webmvc-3.2.0.RELEASE.jar
整合 Spring 与 Mybatis
相干的配置文件
log4j.properties

Rules reminder:
DEBUG < INFO < WARN < ERROR < FATAL

Global logging configuration
log4j.rootLogger=debug,stdout

My logging configuration…
log4j.logger.cn.jbit.mybatisdemo=DEBUG

Console output…
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n

log4j.logger.org.apache.ibatis=DEBUG
log4j.logger.org.apache.jdbc.SimpleDataSource=DEBUG
log4j.logger.org.apache.ibatis.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapclientDelegate=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

myBatis-config.xml 文件

通过 package, 能够间接指定 package 的名字,mybatis 会主动扫描你指定包上面的 javabean, 并且默认设置一个别名,默认的名字为非限定类名来作为它的别名。

<?xml version=”1.0″ encoding=”UTF-8″ ?>
<!DOCTYPE configuration PUBLIC “-//mybatis.org//DTD Config 3.0//EN”
“http://mybatis.org/dtd/mybatis-3-config.dtd”>
<configuration>
<!– 通过别名简化对类的应用 –>
<typeAliases>
<!– <typeAlias type=”cn.itcast.scm.entity.Dept” alias=”Dept” />
–>
<!–

    通过 package, 能够间接指定 package 的名字,mybatis 会主动扫描你指定包上面的 javabean,
  并且默认设置一个别名,默认的名字为非限定类名来作为它的别名。-->

<package name=”zhongfucheng.entity”/>
</typeAliases>

<!– <mappers>

    <mapper resource="cn/itcast/scm/entity/DeptMapper.xml" />
</mappers>
     -->

</configuration>
1.
applicationContext.xml

配置数据源,记得去掉 myBatis-config.xml 的数据源相干配置 
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
    <property name="jdbcUrl"
              value="jdbc:mysql://localhost:3306/scm?useUnicode=true&amp;characterEncoding=UTF-8" />
    <property name="user" value="root" />
    <property name="password" value="root" />
</bean>
<!-- 配置 session 工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:myBatis-config.xml" />
    <!-- 配置扫描式加载 SQL 映射文件, 记得去掉 mybatis-config 配置 -->
<!--    <property name="mapperLocations" value="classpath:zhongfucheng/dao/*.xml"/>-->


</bean>

<!-- 配置事务管理器, 治理数据源事务处理 -->
<bean id="transactionManager"
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置事务告诉 -->
<tx:advice id="advice" transaction-manager="transactionManager">
    <tx:attributes>
        <!-- 默认只解决运行时异样,可加 rollback-for="Exception/Throwable" 等解决所有异样或包含谬误 -->
        <tx:method name="insert*" propagation="REQUIRED"
                   rollback-for="Exception" />
        <tx:method name="update*" propagation="REQUIRED"
                   rollback-for="Exception" />
        <tx:method name="delete*" propagation="REQUIRED"
                   rollback-for="Exception" />
        <tx:method name="*" propagation="SUPPORTS" />
    </tx:attributes>
</tx:advice>
<!-- 配置切面织入的范畴, 后边要把事务边界定在 service 层 -->
<aop:config>
    <aop:advisor advice-ref="advice"
                 pointcut="execution(* cn.itcast.scm.service.impl.*.*(..))" />
</aop:config>
<!-- 配置 SessionTemplate,已封装了繁琐的数据操作 -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

<!-- <context:component-scan base-package="*" /> -->


<!-- 主动扫描组件,要把 controller 去除,他们是在 spring-mvc.xml 中配置,如果不去除会影响事务管理。-->
<context:component-scan base-package="cn.itcast">
    <context:exclude-filter type="annotation"
                            expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<!-- 配置 转换器,对于在 basePackage 设置的包(包含子包)下的接口类,如果接口类的全类名在 Mapper.xml 文件中和定义过命名空间统一,将被转换成 spring 的 BEAN,在调用 
    的中央通过 @Autowired 形式将能够注入接口实例 -->

<!–

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />

    &lt;!&ndash; 这里包名注意要不要改 &ndash;&gt;
    <property name="basePackage" value="zhongfucheng.dao" />
</bean>

–>

</beans>
1
编写实体及 sql 映射游戏文件
SQL 脚本

drop database if exists mybatis;
create database mybatis CHARACTER SET UTF8;
use mybatis;

create table dept(

dept_id int primary key auto_increment,
dept_name varchar(50),
dept_address varchar(50)

);

insert into dept(dept_name,dept_address) values(‘ 研发部一部 ’,’ 广州 ’);
insert into dept(dept_name,dept_address) values(‘ 研发部二部 ’,’ 广州 ’);
insert into dept(dept_name,dept_address) values(‘ 研发部三部 ’,’ 深圳 ’);
select * from dept;

实体:

package zhongfucheng.entity;

import java.io.Serializable;

public class Dept implements Serializable {

private Integer deptId;
private String deptName;
private String deptAddress;
public Integer getDeptId() {return deptId;}
public void setDeptId(Integer deptId) {this.deptId = deptId;}
public String getDeptName() {return deptName;}
public void setDeptName(String deptName) {this.deptName = deptName;}
public String getDeptAddress() {return deptAddress;}
public void setDeptAddress(String deptAddress) {this.deptAddress = deptAddress;}
@Override
public String toString() {
    return "Dept [deptId=" + deptId + ", deptName=" + deptName
            + ", deptAddress=" + deptAddress + "]";
}

}

映射文件

<?xml version=”1.0″ encoding=”UTF-8″ ?>
<!DOCTYPE mapper PUBLIC “-//mybatis.org//DTD Mapper 3.0//EN” “http://mybatis.org/dtd/mybatis-3-mapper.dtd”>

<mapper namespace=”zhongfucheng.entity.DeptMapper”>

<resultMap type="Dewww.cungun.compt" id="deptResultMap">
    <id property="deptId" column="dept_id" />
    <result property="deptName" column="dept_name" />
    <result property="deptAddress" column="dept_address" />
</resultMap>
<!-- id 和命名空间用来定位 SQL 语句,parameterType 示意参数的类型,resultMap 返回类型 -->
<select id="selectDept" parameterType="Integer" resultMap="deptResultMap">
    <!-- 参数的写法 #{deptID} -->
    select * from dept where dept_id=#{deptID}
</select>

<insert id="insertDept" parameterType="Dept">
    insert into dept(dept_name,dept_address) values(#{deptName},#{deptAddress});
</insert>

</mapper>

正文完
 0