IDEA下搭建ssm项目-02

本文介绍如何在IDEA中搭建Spring+SpringMVC+MyBatis(SSM)项目,包括配置application-context.xml、jdbc.properties等文件,以及创建DAO、Service层实现增删改查操作,并提供单元测试示例。

IDEA下搭建ssm项目-01

application-context.xml配置,这里的模板后面就不贴出了
<?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:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

   <!--这里直接 把其他配置分开-->
   <import resource="config/*.xml" />
</beans>
properties目录下

jdbc.properties:
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/你的数据库?characterEncoding=UTF-8
user=root
password=123456

接下来是config目录下的配置文件

annotation.xml:

<!--spring 扫包   @Service .....-->
<context:component-scan base-package="com.nelsoner">
   <!--不扫描 Controller -->
   <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<!--自动装配-->
   <context:annotation-config/>

property.xml

<!-- 读取jdbc配置 -->
<!--这种方式的扩展性不好-->
<!--
   <context:property-placeholder location="classpath:properties/jdbc.properties" />
-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="locations">
      <list>
         <!-- jdbc配置 -->
         <value>classpath:properties/jdbc.properties</value>
         <!-- memcached配置 -->
         
      </list>
   </property>
</bean>

jdbc.xml:

<!-- c3p0-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
   <property name="driverClass" value="${driverClass}"/>
   <property name="jdbcUrl" value="${jdbcUrl}"></property>
   <property name="user" value="${user}" />
   <property name="password" value="${password}"/>
</bean>
mybatis.xml:

<!--mybatis  sessionFactory配置-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
   <property name="dataSource" ref="dataSource"/>
   <!--这里要特别注意 classpath:com.nelsoner.core.dao/**/*.xml  /**/ 表示任意多级目录-->
   <property name="mapperLocations" value="classpath:com.nelsoner.core.dao/**/*.xml"/>
   <property name="typeAliasesPackage" value="com.nelsoner.core.bean"/>
</bean>
<!-- 扫包 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.nelsoner.core.dao"/></bean>
transation.xml:

<!-- spring 事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
到这里基本配置完成

接下来写个测试

TestTb:

private Integer id;
private String name;
private Date birthday;  
//这里省略get set 

TestTbDao:

public interface TestTbDao {
   public void addTestTb(TestTb testTb);
}

TestTbService:

public interface TestTbService {
   public void addTestTb(TestTb testTb);
}

TestTbServiceImpl:

@Service
@Transactional
public class TestTbServiceImpl implements TestTbService{
   @Resource
   private TestTbDao testTbDao;
   public void addTestTb(TestTb testTb) {
      testTbDao.addTestTb(testTb);
   }
}

TestTbDao.xml:

<?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="com.nelsoner.core.dao.TestTbDao">
   <!--  添加测试数据  -->
   <!-- id 就是TestTbDao类的函数名-->
   <!--
      在mybatis.xml 中配置了如下: 所以 parameterType可以不写前面的包
      <property name="typeAliasesPackage" value="com.nelsoner.core.bean"/>
   -->
   <insert id="addTestTb" parameterType="TestTb">
      insert into test_tb
      (name,birthday)
      values
      (#{name},#{birthday})
   </insert>
</mapper>


接下来写测试方法

基于注解的Junit单元测试


TestTestTb:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:application-context.xml")
public class TestTestTb{

   @Autowired
   private TestTbService testTbService;
   @Test
   public void testAdd() throws Exception {
      TestTb testTb = new TestTb();
      testTb.setName("金乐乐");
      
      testTbService.addTestTb(testTb);
   }
}


项目目录结构:


这个是完整源码 python实现 Flask,Vue 【python毕业设计】基于Python的Flask+Vue物业管理系统 源码+论文+sql脚本 完整版 数据库是mysql 本文首先实现了基于Python的Flask+Vue物业管理系统技术的发展随后依照传统的软件开发流程,最先为系统挑选适用的言语和软件开发平台,依据需求分析开展控制模块制做和数据库查询构造设计,随后依据系统整体功能模块的设计,制作系统的功能模块图、E-R图。随后,设计框架,依据设计的框架撰写编码,完成系统的每个功能模块。最终,对基本系统开展了检测,包含软件性能测试、单元测试和性能指标。测试结果表明,该系统能够实现所需的功能,运行状况尚可并无明显缺点。本文首先实现了基于Python的Flask+Vue物业管理系统技术的发展随后依照传统的软件开发流程,最先为系统挑选适用的言语和软件开发平台,依据需求分析开展控制模块制做和数据库查询构造设计,随后依据系统整体功能模块的设计,制作系统的功能模块图、E-R图。随后,设计框架,依据设计的框架撰写编码,完成系统的每个功能模块。最终,对基本系统开展了检测,包含软件性能测试、单元测试和性能指标。测试结果表明,该系统能够实现所需的功能,运行状况尚可并无明显缺点。本文首先实现了基于Python的Flask+Vue物业管理系统技术的发展随后依照传统的软件开发流程,最先为系统挑选适用的言语和软件开发平台,依据需求分析开展控制模块制做和数据库查询构造设计,随后依据系统整体功能模块的设计,制作系统的功能模块图、E-R图。随后,设计框架,依据设计的框架撰写编码,完成系统的每个功能模块。最终,对基本系统开展了检测,包含软件性能测试、单元测试和性能指标。测试结果表明,该系统能够实现所需的功能,运行状况尚可并无明显缺点。本文首先实现了基于Python的Flask+Vue物业管理系统技术的发
源码地址: https://pan.quark.cn/s/a4b39357ea24 # SerialAssistant串口助手 下载地址: 本仓库release文件夹 在线下载:http://mculover666.cn/SerialAssistant.zip 功能说明 本项目是使用C# + WinForm框架编写的串口助手。 目前版本为2.0.0版本,拥有以下功能: 未打开串口时,自动扫描可用端口 接收数据支持文本或者HEX方式显示 支持接收数据加入时间戳 支持将当前接收数据保存为文件 支持发送文本数据或HEX数据 支持自动定时发送数据 支持从文件中(.txt, .json)加载数据到发送文本框 支持发送数据记录(不重复记录) ……欢迎加入更多功能 环境说明 VS2019 .NET Framework 4.5 教程 C#上位机开发(一)—— 了解上位机 C#上位机开发(二)—— Hello,World C#上位机开发(三)—— 构建SerialAssistant雏形 C#上位机开发(四)—— SerialAssistant功能完善 C#上位机开发(五)——SerialAssistant界面升级(WinForm界面布局进阶) C#上位机开发(六)——SerialAssistant功能优化(串口自动扫描功能、接收数据保存功能、加载发送文件、发送历史记录、打开浏览器功能、定时发送功能) C#上位机开发(七)—— 修改窗口图标和exe文件图标 C#上位机开发(八)—— 美化界面(给按钮添加背景) 更新日志 2018/6/3 完成串口属性设置,打开与关闭异常处理; 字符串发送功能; 字符串接收功能; 2018/6/4 完善串口扩展功能界面部分 2018/6/6 完善...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值