Spring_xml和注解混合方式開發


1. spring核心配置文件:

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 讀取占位符配置文件,和mybatis的一樣  -->
    <context:property-placeholder location="classpath:db.properties" />
    
    <!-- apache的連接池依賴類,pom里配置引入commons-dbcp -->
    <!-- 這里需要占位符配置url、username、password -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${db.driver}" />
        <property name="url" value="${db.url}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
    </bean>
    
    <!-- spring提供的jdbc template方便dao層編寫,pom引入依賴spring-jdbc -->
    <!-- 這里需要注入上面生成好的數據庫連接池dataSource -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 注解掃描dao和service類 -->
    <!-- 該組件掃描器,會去掃描@Component、@Controller、@Service、@Repository、@RestController -->
    <context:component-scan base-package="amie"></context:component-scan>

</beans>

2.POJO類

public class User {
    private int id;

    private String username;

    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", address=" + address + "]";
    }

    private String address;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

3. dao類

@Repository
public class daoAnnoImpl implements userDao {
  
    //自動裝配
    @Resource(name = "jdbcTemplate")
    private JdbcTemplate template;

    public JdbcTemplate getTemplate() {
        return template;
    }

    public void setTemplate(JdbcTemplate template) {
        this.template = template;
    }

    @Override
    public User getUserById(int id) {
        // 業務SQL
        String sql = "SELECT * FROM user WHERE id = ?";
        // 參數數組
        Object[] args = { id };
        // template類中沒有insert方法,直接調用update
        return template.queryForObject(sql, args, new RowMapper<User>() {

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

4.service類

@Service
public class annoImpl implements userService{
    
    //自動裝配
    @Autowired
    private daoAnnoImpl dao;
    
    public daoAnnoImpl getDao() {
        return dao;
    }

    public void setDao(daoAnnoImpl dao) {
        this.dao = dao;
    }

    @Override
    public User findUser(int id) {
        return dao.getUserById(id);
    }
}

5.使用類(這里用servlet)

public class userView extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        // 獲取參數
        String id = request.getParameter("id");
        
        // 每次doGet都生成一次context
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-anno.xml");
        
         // 通過Spring IOC容器獲取 service bean
        userService service = context.getBean(userService.class);

        // 調用service的方法,獲取user
        User user = service.findUser(Integer.parseInt(id));

        // 響應,打印user
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().print(user);
        
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM