Spring_xml和注解混合方式开发

时间:2023-03-09 02:49:25
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); } }