目录结构
tars生成的文件当成 controller 来调用 service ,service 调用 mapper
POM
注意如果 mybatis是3.4.1 spring 是4.1.14的话, mybatis-spring 整合包必须是1.3.0
<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.wepai</groupId> <artifactId>tars-SSM-server2</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>tars-SSM-server2 Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spirng.version>4.3.14.RELEASE</spirng.version> <spirngmvc.version>4.3.14.RELEASE</spirngmvc.version> <mybatis.version>3.4.1</mybatis.version> <ms.version>1.3.0</ms.version> <jackson.version>2.9.5</jackson.version> <dbcp.version>1.4</dbcp.version> <mysql.version>5.1.38</mysql.version> <servlet.version>3.0.20100224</servlet.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- tars-spring --> <dependency> <groupId>com.tencent.tars</groupId> <artifactId>tars-spring</artifactId> <version>1.4.0</version> </dependency> <!-- tars-spring jar 不完全 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spirng.version}</version> </dependency> <!-- Servlet --> <dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>servlet-api</artifactId> <version>${servlet.version}</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${ms.version}</version> </dependency> <!-- springMVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spirngmvc.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spirngmvc.version}</version> </dependency> <!-- jackson --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency> <!-- dbcp --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>${dbcp.version}</version> </dependency> </dependencies> <build> <finalName>tars-SSM-server2</finalName> <plugins> <plugin> <groupId>com.tencent.tars</groupId> <artifactId>tars-maven-plugin</artifactId> <version>1.4.0</version> <configuration> <tars2JavaConfig> <!-- 项目中 tars 文件位置 --> <tarsFiles> <tarsFile>${basedir}/src/main/resources/user.tars</tarsFile> </tarsFiles> <!-- 源文件编码 --> <tarsFileCharset>UTF-8</tarsFileCharset> <!-- 生成服务端代码 --> <servant>true</servant> <!-- 生成源代码编码 --> <charset>UTF-8</charset> <!-- 生成的源代码目录 --> <srcPath>${basedir}/src/main/java</srcPath> <!-- 生成源代码包前缀 --> <packagePrefixName>com.weipai.server.</packagePrefixName> </tars2JavaConfig> </configuration> </plugin> <!-- jdk 版本 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> <!-- mybatis xml 文件和 tars 文件打包到war --> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> <include>**/*.tars</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
tar 文件
module User { interface User{ void insert(String guid, String name, int sex); string hello(int no, string name); }; };
servants-spring.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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tars="http://tars.tencent.com/schema/tars" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://tars.tencent.com/schema/tars http://tars.tencent.com/schema/tars/tars.xsd"> <!-- 扫描,支持spring注解 --> <context:component-scan base-package="com.weipai.service.impl,com.weipai.server.user.impl,com.weipai.pojo" /> <!-- 暴露 tars 服务 --> <bean id="UserServantImpl" class="com.weipai.server.user.impl.UserServantImpl" /> <tars:servant name="UserObj" interface="com.weipai.server.user.UserServant" ref="UserServantImpl"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="WeiPai_111111"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:spring-mybatis.xml" /> <property name="typeAliasesPackage" value="com.weipai.pojo" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.weipai.mapper" /> </bean> </beans>
spring-mybatis.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> <!-- 配置延迟加载 --> <settings> <!-- 打开延迟加载 --> <setting name="lazyLoadingEnabled" value="true" /> <!-- 延迟加载的方式:按需加载 --> <setting name="aggressiveLazyLoading" value="false"/> </settings> </configuration>
service接口
和 mybatis 生成的接口一致
service实现类
tars 实现类
要实现 tars 接口
然后就可以发布了