mybatis配置
转载请申明转载出处:http://blog.csdn.net/qq5132834/article/details/52227666
在上一节中提到mybatis的一个重要工具【mybatis-generator】的使用方式,本节将涉及【mybatis】的配置了。本节的内容代码是在第五节【将spring、spring mvc的配置文件分开】的基础上继续编写,下载地址:http://download.csdn.net/detail/qq5132834/9602553
1、引入【mybatis】必须的jar包,分别是:
mybatis-3.1.1.jar
mybatis-spring-1.1.1.jar
druid-0.2.10.jar
aspectjweaver-1.7.0.jar
aopalliance-1.0.jar
mysql-connector-java-5.1.21.jar。
这以上jar包添加至【WEB-INF/lib】中。
2、根据上一讲中提到的在【src】中新建三个包:【com.soft.model】、【com.soft.mapping 】、【sqlmap】
3、在【src/sources】文件夹中加入【spring-mybatis.xml】,内容如下:
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">
<!-- JNDI方式配置数据源 -->
<!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> -->
<!-- 配置数据源 -->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- mybatis文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描entity目录,省略Configuration.xml里手工配置 -->
<property name="mapperLocations" value="classpath:sqlmap/*.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.soft.mapping" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!-- 配置事务管理器 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 注解方式配置事物 -->
<!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->
<!-- 拦截器方式配置事物 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="append*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="repair" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="load*" propagation="REQUIRED" read-only="true" />
<tx:method name="search*" propagation="REQUIRED" read-only="true" />
<tx:method name="datagrid*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* com.zuk.services..*Impl.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
</aop:config>
</beans>
注意【spring-mubatis.xml】文件中的内容:
3.1、数据源
<!-- 配置数据源 -->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
如果不想添加【druid-0.2.10.jar】包,则使用springframework的数据库连接代替亦可。
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- mybatis文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描entity目录,省略Configuration.xml里手工配置 -->
<property name="mapperLocations" value="classpath:sqlmap/*.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.soft.mapping" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
3.3、这个配置不知道要不要配置,但是要配置,也不会报错。
注意:【expression = "execution(* com.zuk.services..*Impl.*(..))"】要与【spring.xml】中的文件配置【<context:component-scan base-package="com.zuk.services" />】配置的内容一致:
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* com.zuk.services..*Impl.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
</aop:config>
4、将【spring-mybiatis.xml】文件添加至【web.xml】文件中,文件路径是:【classpath:sources/spring-mybatis.xml】
<!-- 加载spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:sources/spring.xml,classpath:sources/spring-mybatis.xml</param-value>
</context-param>
5、将上一节中通过【mybatis-generator】工具产生java、xml文件,复制到对应的包中。
6、在【com.zuk.services】中新建一个【LoginService.java】类。代码如下:
package com.zuk.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.soft.mapping.LoginMapper;
import com.soft.model.Login;
@Service("LoginService")
public class LoginService {
@Autowired
private LoginMapper loginMapper;
public Login getLogin(String userId){
Login login = this.loginMapper.selectByPrimaryKey(userId);
return login;
}
}
7、修改【LoginController.java】类,在其中引入数据如下:
@Autowired
public LoginService loginService;
完整代码如下:
package com.zuk.controllers;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.nutz.json.Json;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.soft.model.Login;
import com.zuk.model.User;
import com.zuk.services.LoginService;
import com.zuk.services.UserService;
/**
* @author 513283439@qq.com
* */
@Controller
@RequestMapping(value="LoginController")
public class LoginController {
@Autowired
public UserService userService;
@Autowired
public LoginService loginService;
@RequestMapping(value = "/login.xhtml")
public String login(HttpSession ession,
HttpServletRequest request,
HttpServletResponse response,
Model model){
return "/jsp/login";
/**
* 跳转的实际路径要算上mvc-servlet.xml中配置的
* 前缀:/WEB-INF;
* 后缀:.jsp
* 实际路径:/WEB-INF/jsp/login.jsp
* */
}
/**
* <br>主要解决两个问题:
* <br>1、POST方法中文乱码问题;
* <br>2、将前端表单里面的name属性的值直接映射到java对象中。
* */
@RequestMapping(value = "/actionForm.xhtml")
public String actionForm(HttpSession ession,
HttpServletRequest request,
HttpServletResponse response,
Model model,
@ModelAttribute User user
){
//System.out.println(person.getUsid());
//System.out.println(person.getPawd());
//System.out.println(person.getComp());
model.addAttribute("usid", user.getUsid());
model.addAttribute("pawd", user.getPawd());
model.addAttribute("comp", user.getComp());
if(this.userService.isExist(user.getUsid(), user.getPawd())){
return "/jsp/main";
}else{
return "/jsp/error";
}
}
/**
*<br>主要解决ajax请求返回json数据烈性
* */
@ResponseBody
@RequestMapping(value = "/ajaxForm.json", produces = "text/html;charset=UTF-8")
public String ajaxForm(HttpSession ession,
HttpServletRequest request,
HttpServletResponse response,
Model model,
@ModelAttribute User user
){
//System.out.println(person.getUsid());
//System.out.println(person.getPawd());
//System.out.println(person.getComp());
model.addAttribute("usid", user.getUsid());
model.addAttribute("pawd", user.getPawd());
model.addAttribute("comp", user.getComp());
NutMap result = NutMap.NEW();
result.put("data", user);
if(this.userService.isExist(user.getUsid(), user.getPawd())){
result.put("success", true);
}else{
result.put("success", false);
}
Login login = this.loginService.getLogin("b180121");
result.put("login", login);
String json = Json.toJson(result);
System.out.println(json);
return json;
}
}
8、发布项目,输入链接:http://localhost:8080/ZZZ/LoginController/login.xhtml 点击【ajax】按钮。
9、源代码:http://download.csdn.net/detail/qq5132834/9607606
10、最后,对于数据库的配置,可以参考【第五节】。