spring和mybits的简单整合测试
1、先配置web.xml
<!-- 初始化spring的转发器,使访问.do的所有操作,都进行跳转 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
2、根据web.xml的<param-value>classpath:spring-mvc.xml</param-value>来新建一个xml,进行spring的配置
<!-- 开启自动扫描 -->
<context:component-scan base-package="com.zmc.*"></context:component-scan>
<!-- 处理请求转发 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property value="/" name="prefix"/>
<property value=".jsp" name="suffix"/>
</bean>
<!-- 配置DBCP数据源 -->
<bean destroy-method="close" id="dbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"></property>
<property name="username" value="system"></property>
<property name="password" value="zmc"></property>
</bean>
<!-- mybatis实现的spring的jdbc接口,实现实体类属性与数据库的自动对应 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dbcpDataSource"></property>
<property name="mapperLocations" value="classpath:com/zmc/**/*.xml"></property>
</bean>
<!-- mybatis自动扫描接口Mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property value="com.zmc.entity" name="basePackage"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
3、最后就是根据需求,自己写dao、service、controller
实体类需要有get和set方法
package com.zmc.entity;
public class User {
private String stId;
private String stAccount;
private String stPWD;
public String getStId() {
return stId;
}
public void setStId(String stId) {
this.stId = stId;
}
public String getStAccount() {
return stAccount;
}
public void setStAccount(String stAccount) {
this.stAccount = stAccount;
}
public String getStPWD() {
return stPWD;
}
public void setStPWD(String stPWD) {
this.stPWD = stPWD;
}
}
dao只要写一个接口就行了
package com.zmc.entity;
public interface UserDao {
public void insertUser(User user);
}
dao.xml具体的sql就是在这里了
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.zmc.entity.UserDao">
<insert id="insertUser" parameterType="com.zmc.entity.User">
insert into t_user(st_id,st_account,st_pwd)
values(#{stId},#{stAccount},#{stPWD})
</insert>
</mapper>
service
package com.zmc.service;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.zmc.entity.User;
import com.zmc.entity.UserDao;
@Service
public class UserService {
@Resource
private UserDao userDao;
public void insertUser(User user){
userDao.insertUser(user);
}
}
controller
package com.zmc.test;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.zmc.entity.User;
import com.zmc.service.UserService;
@Controller
public class TestController {
@Autowired
private UserService userService;
@RequestMapping("/test.do")
public ModelAndView select(){
User user=new User();
user.setStId(UUID.randomUUID().toString());
user.setStAccount("zmc");
user.setStPWD("zmc");
userService.insertUser(user);
return new ModelAndView("index");
}
}