Spring jpa存储库JNDI数据源

时间:2016-03-07 10:25:51

标签: java spring-mvc jpa spring-data jndi

嘿大家都在使用spring MVC而且我想要将JPA Repository与JNDI Data Source集成,我有一个没有jpa存储库的app我的代码:

创建我的数据源的mvcConfiguration类:

package net.codejava.spring.config;

import javax.naming.NamingException;
import javax.sql.DataSource;

import net.codejava.spring.dao.UserDAO;
import net.codejava.spring.dao.UserDAOImpl;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jndi.JndiTemplate;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import   org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@ComponentScan(basePackages="net.codejava.spring")
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{

@Bean
public ViewResolver getViewResolver(){
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

@Bean
public UserDAO getUserDao() throws NamingException {
    JndiTemplate jndiTemplate = new JndiTemplate();
    DataSource dataSource = (DataSource)   jndiTemplate.lookup("java:comp/env/jdbc/postgres2");
    return new UserDAOImpl(dataSource);
}

}

这个我想用Jpa存储库替换的Dao层

package net.codejava.spring.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import net.codejava.spring.model.User;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

public class UserDAOImpl implements UserDAO {
private DataSource dataSource;

public UserDAOImpl(DataSource dataSource) {
    this.dataSource = dataSource;
}

@Override
public List<User> list() {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    String sql = "SELECT * from users";
    List<User> listUser = jdbcTemplate.query(sql, new RowMapper<User>() {

        @Override
        public User mapRow(ResultSet rs, int rowNumber) throws SQLException  {
            User user = new User();
            user.setId(rs.getInt("id"));
            user.setUsername(rs.getString("username"));
            user.setEmail(rs.getString("email"));
            return user;
        }

    });
    return listUser;
}

}

和context.xml

<Context>

    <WatchedResource>WEB-INF/web.xml</WatchedResource>    
<Resource name="jdbc/postgres2" auth="Container"
      type="javax.sql.DataSource" driverClassName="org.postgresql.Driver"
      url="jdbc:postgresql://127.0.0.1:5432/mydb"
      username="postgres" password="root" maxActive="20" maxIdle="10" maxWait="-1"/>



 </Context>

感谢任何指南..

0 个答案:

没有答案