一、项目建立
1、使用eclipse建立maven项目“YYSchedule-parent”
2、建立项目YYSchedule-common
2、建立项目YYSchedule-storemanager
3、建立项目YYSchedule-taskmanager
4、建立项目YYSchedule-nodemanager
二、在“YYSchedule-parent”中添加依赖(详见pom.xml),注意要将打包方式设置成pom
在“YYSchedule-storemanager”中添加依赖,父工程为parent
在“YYSchedule-taskmanager”中添加依赖,父工程为parent
在“YYSchedule-nodemanager”中添加依赖,父工程为parent
三、mybatis自动生成mapper(也就是dao层)
1、使用自动生成工具生成mapper文件和pojo文件。
2、mapper文件夹的文件一起放在storemanager下,pojo文件夹放在common文件夹下
最最最重要的一点!!
在storemanager的pom文件中写入如下:
<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**.*</include> </includes> </resource> </resources>
四、在taskmanager项目中配置spring等配置文件
src/main/resources文件夹中创建mybatis文件夹,properties文件夹以及spring文件夹(记得把resources文件夹add to build path)
并在里面各自创建配置文件,如下图所示:
SqlMapConfig.xml:
<?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> <plugins> <!-- com.github.pagehelper为PageHelper类所在包名 --> <plugin interceptor="com.github.pagehelper.PageHelper"> <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库--> <property name="dialect" value="mysql"/> </plugin> </plugins> </configuration>
db.properties:
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://192.168.25.134:3306/taotao?characterEncoding=utf-8 jdbc.username=root jdbc.password=root
applicationContext-dao.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/aop http://www.springframework.org/schema/aop/spring-aop-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/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 数据库连接池 --> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:properties/*.properties"/> <!-- 数据库连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" 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="minIdle" value="5" /> </bean> <!-- 配置SqlsessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 加载mybatis的配置文件 --> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/> <!-- 配置数据源 --> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置包扫描器,扫描mapper接口生成代理对象放到spring容器中 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 指定要扫描的包 --> <property name="basePackage" value="com.binto.YYSchedule.storemanager.mapper"/> </bean> </beans>applicationContext-service.xml:(其他地方不变,所以)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/aop http://www.springframework.org/schema/aop/spring-aop-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/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 配置包扫描器,扫描@Service主键的类 --> <context:component-scan base-package="com.binto.YYSchedule.storemanager.service"/> </beans>
applicationContext-dao.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/aop http://www.springframework.org/schema/aop/spring-aop-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/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 事务配置 --> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 数据源 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 传播行为 --> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> <tx:method name="select*" propagation="SUPPORTS" read-only="true" /> <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> <!-- 切面 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.binto.YYSchedule.storemanager.service.*.*(..))" /> </aop:config> </beans>
五、在storemanager中创建service层
1、将common模块引入到storemanager模块中,方法:
(File-project structure-选择storemanager-dependencies-“+”号-module dependency-选择common点击ok)
2、创建package,com.binto.schedule.storemanager.service以及xxxx.service.impl
3、编写UserBasicService类以及UserBasicServiceImpl
UserBasicServiceImpl类:(注意一定要有@Service注解!)
@Service public class UserBasicServiceImpl implements UserBasicService { @Resource UserBasicMapper userBasicMapper; @Override public UserBasic getUserBasicMapperById(Integer userId) { UserBasicExample userBasicExample = new UserBasicExample(); UserBasicExample.Criteria criteria = userBasicExample.createCriteria(); criteria.andUserIdEqualTo(1); List<UserBasic> userBasicList = userBasicMapper.selectByExample(userBasicExample); UserBasic userBasic = null; if(userBasicList != null && userBasicList.size() > 0 ) { userBasic = userBasicList.get(0); } return userBasic; } }4、在taskmanager中编写测试类,用于测试
public class UserBasicServiceTest { private ApplicationContext applicationContext; @Before public void init() { //创建一个spring容器 applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml"); } @Test public void testGetUserBasicMapperById() { UserBasicService userBasicService = applicationContext.getBean(UserBasicService.class); int userId = 1; UserBasic userBasic= userBasicService.getUserBasicMapperById(userId); System.out.println(userBasic.getUsername()); } }
测试成功!