guice + mybatis+ resteasy mybatisresteasyguice
程序员文章站
2022-07-16 22:27:19
...
web.xml
<web-app> <display-name>Guice Hello</display-name> <context-param> <param-name>resteasy.guice.modules</param-name> <param-value>org.jboss.resteasy.examples.guice.hello.HelloModule,org.jboss.resteasy.examples.guice.hello.MybaitsModule</param-value> </context-param> <context-param> <param-name>resteasy.servlet.mapping.prefix</param-name> <param-value>/resteasy</param-value> </context-param> <listener> <listener-class> org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener </listener-class> </listener> <servlet> <servlet-name>Resteasy</servlet-name> <servlet-class> org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher </servlet-class> </servlet> <servlet-mapping> <servlet-name>Resteasy</servlet-name> <url-pattern>/resteasy/*</url-pattern> </servlet-mapping> </web-app>
Module.java
import java.util.Properties; import com.google.inject.Binder; import com.google.inject.Module; import com.google.inject.name.Names; public class HelloModule implements Module { public void configure(final Binder binder) { Properties myBatisProperties = new Properties(); myBatisProperties.setProperty("mybatis.environment.id", "test"); myBatisProperties.setProperty("JDBC.schema", "test"); myBatisProperties.setProperty("JDBC.driver", "com.mysql.jdbc.Driver"); myBatisProperties.setProperty("JDBC.host", "127.0.0.1"); // myBatisProperties.setProperty("derby.create", "true"); myBatisProperties.setProperty("JDBC.username", "root"); myBatisProperties.setProperty("JDBC.password", "1"); myBatisProperties.setProperty("JDBC.autoCommit", "false"); Names.bindProperties(binder, myBatisProperties); binder.bind(HelloResource.class); binder.bind(Greeter.class).to(DefaultGreeter.class); } }
MybaitsModule.java
public class MybaitsModule extends MyBatisModule { @Override protected void initialize() { // mysql install(JdbcHelper.MySQL); // DataSource bindDataSourceProviderType(PooledDataSourceProvider.class); bindTransactionFactoryType(JdbcTransactionFactory.class); // 数据库操作 addMapperClass(UserMapper.class); // 逻辑操作 bind(FooService.class).to(FooServiceMapperImpl.class); } }