1. 程式人生 > >SpringMvc+Spring+Mybatis 整合

SpringMvc+Spring+Mybatis 整合

1.導jar包
2.整合mybatis+Spring
①建表
②dao
③spring.xml

	<!-- 開啟註解掃描 --><!-- service 位置 -->
	<context:component-scan base-package="com.yangyang" />
	<!-- 引入小配置檔案 -->
	<context:property-placeholder location="classpath:jdbc.properties" />
	<!-- 建立資料來源 -->
	<bean id
="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.drivcer}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.name}"/> <property name="password" value="${jdbc.password}"/> </
bean
>
<!-- 建立sqlsessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" > <property name="dataSource" ref="dataSource" /> <property name="typeAliasesPackage" value="com.yangyang.entity" /> <property name="mapperLocations" value
="classpath:com/yangyang/mapper/*Mapper.xml" />
</bean> <!-- 建立DAO物件 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <property name="basePackage" value="com.yangyang.dao" /> </bean> <!-- 建立事務管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name="dataSource" ref="dataSource" /> </bean> <!-- 開啟註解式事務生效 --> <tx:annotation-driven transaction-manager="transactionManager" />

④開發Mapper.xml
⑤開發Service

@Service("userService")
@Transactional
public class UserServiceIMP implements UserService {
	//貌似是自動注入
	@Autowired
	private UserDAO userDAO;

	public void add(User user) {
		user.setId(UUID.randomUUID().toString());
		System.out.println(user);
		userDAO.save(user);
	}

	

	@Override
	public User questId(String id) {
		User questId = userDAO.questId(id);
		return questId;
	}


⑥Test測試類

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class UserTest {
	@Autowired
	private UserService userService;
	@Autowired
	private Z_UserService z_UserService;
	@Test
	public void Test1(){
		List<User> quesrALL = userService.quesrALL();
		System.out.println(quesrALL);
	}
}

3.加入Spring開發

		<!-- 開啟註解掃描 -->
		<context:component-scan base-package="com.yangyang.controller"/>
		<!-- 註冊處理器介面卡和處理對映器 -->
		<mvc:annotation-driven></mvc:annotation-driven>
		<!-- 配置檢視解析器 -->
		<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
			<property name="prefix" value="/ems1.0/"/>
			<property name="suffix" value=".jsp"/>
		</bean>
		<!-- 處理靜態資源攔截問題 -->
		<mvc:default-servlet-handler/>	

②建立包 controller

@Controller
@RequestMapping("user")
public class UserController {
	@Autowired
	private UserService userService;
	@RequestMapping("list")
	public String questALL(HttpServletRequest request,HttpServletResponse response){
		List<User> quesrALL = userService.quesrALL();
		request.setAttribute("list", quesrALL);
		return "emplist";
	}
	@RequestMapping("update")
	public String update(HttpServletRequest request,HttpServletResponse response,User user){
		System.out.println(user);
		userService.update(user);
		return "redirect:/user/list";
	}

③配置web.xml

<!-- 配置springmvc核心servlet -->
  <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:springmvc.xml</param-value>
	  </init-param>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
  
  <!-- 解決post方式中文亂碼問題 -->
  <filter>
  	<filter-name>encoding</filter-name>
  	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  	<init-param>
  		<param-name>encoding</param-name>
  		<param-value>UTF-8</param-value>
  	</init-param>
  	<init-param>
  		<param-name>forceEncoding</param-name>
  		<param-value>true</param-value>
  	</init-param>
  </filter>
  
  <filter-mapping>
  	<filter-name>encoding</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- 啟動spring工廠監聽器 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 載入工廠配置檔案所在位置 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:spring.xml</param-value>
  </context-param>