没有什么不可能 之前一直用的是自己搭建的spring 和mybatis的框架 但是里面有很多不足
1:mybatis下面的sql使用的mapper xml格式的时候 过于烦了 都要在xml下面修改 然后从启服务。
2:在使用注解的时候 每次都要配置mapperInterface。 在applicationContext这个配置文件里面也好生麻烦。
下面就简单的来解决一下这个问题。
spring-mybatis写业务的时候 全注解的方式 使用,
首先 准备工作 开启本地的mysql数据库 或者是链接好服务器的mysql数据库 把里面的链接配置都拿出来
新建一个web工程
在项目的lib下面 把整理好的spring mybatis mysql的jar全部拷贝到下面
新建一个pojo
import java.io.Serializable;
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private int userid;
private String username;
private String password;
private String createtime;
private String updatetime;
private String telephone;
private int status;
private String nickname;
private int integral;
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public String getUpdatetime() {
return updatetime;
}
public void setUpdatetime(String updatetime) {
this.updatetime = updatetime;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public int getIntegral() {
return integral;
}
public void setIntegral(int integral) {
this.integral = integral;
}
}
新建一个UserProvider类 写一个查询全部的方法 这个类主要就是返回sql 留给mybatis去执行
public class UserProvider {
/**
* 在这个类下面编写user岁要用的sql
*
* 更新时间 2015年5月18日13:46:00
*
* 新浪微博 疯狂的杨中仁
*/
// 使用和mapperxml格式是一样的格式
private static final String COLUMNS = " USERID,USERNAME,PASSWORD,CREATETIME,UPDATETIME,TELEPHONE,STATUS,NICKNAME,INTEGRAL ";
private static final String TABLENAME = "user";
// 查询所有的sql 所有的方法返回一个sql就可以 这边的方法名称和mapper下面的属性的method是一致的
// 2015年5月18日13:47:05
public String all() {
return Variable.DB_SELECT + COLUMNS + Variable.DB_FROM + TABLENAME;
}
public String list(Map<String, Object> map){
StringBuffer sqlBuffer = new StringBuffer();
sqlBuffer.append(Variable.DB_SELECT + COLUMNS + Variable.DB_FROM + TABLENAME);
// 传过来的参数是 map user
User user = (User) map.get("user");
if (user!=null) {
sqlBuffer.append(Variable.DB_WHERE + " 1 = 1 ");
// 拼接下面的status使用 条件
sqlBuffer.append(Variable.DB_AND + " status = " + user.getStatus());
}
return sqlBuffer.toString(); // 最终拼接好的sql返回出去
}
}
在新建一个usermapper 这边使用@param注解 是在sql类下面 使用的是map来取值
import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;
// 配置缓存
@CacheNamespace(size = 512)
public abstract interface UserMapper{
/**
* 使用注解的方式来调用type下面的数据库操作的方法
*
* 更新时间 2015年5月18日13:41:21
*
* 新浪微博 @ 疯狂的杨中仁
*/
// 查询所有的对象 2015年5月18日13:42:11
@SelectProvider(type = UserProvider.class, method = "all")
@Options(useCache = true, flushCache = false, timeout = 10000) // 请求时间是10秒
// @Results({
// @Result(id=true,property="userid",column="userid"),
// }) // 如果有什么不一样的 可以写在这个上面 如果都是一样的 那么就不要写
public abstract List<User> all();
// 根据传过来的user对象查询 2015年5月18日13:45:07
@SelectProvider(type = UserProvider.class, method = "list")
@Options(useCache = true, flushCache = false, timeout = 10000) // 请求时间是10秒
public abstract List<User> list(@Param("user") User user);
}
新建一个service类 这边使用@service注解 是讲这个对象放在spring容器里面
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> all(){
return userMapper.all();
}
}
新建一个controller类 使用controller注解 也是放在springmvc的容器里面 @resquestmapper 使用的是路径 使用boddy注解 是讲这个类作为json格式放回。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller()
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("all")
@ResponseBody
public List<User> all(){
return userService.all();
}
}
这样一个简单的mvc框架基本就完成了 下面 就是配置文件了 applicationContext 下面配置数据源和 注解要扫描的包
讲这个配置文件放在 web-inf下面
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">
<context:component-scan base-package="me.note.pro"></context:component-scan>
<bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor">
</bean>
<!-- 配置 BasicDataSource -->
<bean id="basicDataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3310/data"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="defaultAutoCommit" value="false"></property>
<!-- configuration the dataSource property -->
</bean>
<!-- 配置 数据库 事物 -->
<bean id="dataSourceTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="basicDataSource"></property>
</bean>
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="basicDataSource"></property>
</bean>
<!-- 开启事务注解驱动 -->
<tx:annotation-driven />
<!--
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactoryBean"></constructor-arg>
</bean>配置 sqlSessionTemplate -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定要扫描包: 多个包用逗号隔开 -->
<property name="basePackage" value="me.note.pro.mapper" />
<!--指定sqlSessionFactory -->
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"></property>
</bean>
<!--
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean" />
<property name="mapperInterface" value="me.note.pro.mapper.UserMapper" />
</bean>
-->
</beans>
开始使用的是下面的注解 后来看了一下源码 打算写一个类 模仿一下MapperFactoryBean 后来发现上面的父类 可以扫描 就换了一下。
下面是springmvc的配置文件
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">
<!-- 配置包名下的配置文件 -->
<context:component-scan base-package="me.note.pro"></context:component-scan>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean id="utf8StringHttpMessageConverter"
class="me.note.spring.framework.UTF8StringHttpMessageConverter" />
<ref bean="mappingJacksonHttpMessageConverter"/>
</list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</beans>
因为使用spring3.2会有乱码的问题 下面贴一下UTF8StringHttpMessageConverter的源码
下面也有我的文章处理了这个问题 这个文档写不下了。