当前位置: 移动技术网 > IT编程>开发语言>Java > 如何将mybatis配置到springmvc中

如何将mybatis配置到springmvc中

2019年07月19日  | 移动技术网IT编程  | 我要评论
mybatis简介 mybatis 是一款优秀的持久层框架,它支持定制化 sql、存储过程以及高级映射。mybatis 避免了几乎所有的 jdbc 代码和手动设置参数以及

mybatis简介

mybatis 是一款优秀的持久层框架,它支持定制化 sql、存储过程以及高级映射。mybatis 避免了几乎所有的 jdbc 代码和手动设置参数以及获取结果集。mybatis 可以使用简单的 xml 或注解来配置和映射原生信息,将接口和 java 的 pojos(plain old java objects,普通的 java对象)映射成数据库中的记录。

为了更方便的连接数据库,将mybatis配置到springmvc中

1).  首先是jar包    多了3个jar  druid 这个是阿里的数据库连接包      mybatis和 mybatis-spring 

2)  然后是项目目录

3)在web.xml中 加上一个spring的配置文件

<context-param></context-param>元素含有一对参数名和参数值,用作应用的servlet上下文初始化参数。参数名在整个web应用中必须是惟一的。设定web应用的环境参数(context)

4)

  spring-mvc的内容不变,spring-mybatis中的内容如下

<!-- mybatis配置 这个就是spring和mybatis的整合 也就是spring-mybatis jar-->
<bean id="mysqlsqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
<!--数据库 多数据源配置多个-->
<property name="datasource" ref="mysqldatasource" />
<!-- 自动扫描mapping.xml文件 -->
<!-- 自动扫描entity目录, 省掉xml里的手工配置 
该属性可以给包中的类注册别名,注册后可以直接使用类名,而不用使用全限定的类名-->
<property name="typealiasespackage" value="com.springmvc.model" />
<!-- mysqlsqlsessionfactory会自动扫描该路径下的所有文件并解析。-->
<property name="mapperlocations">
<list>
<value>classpath:/mybatis/*mapper.xml</value>
</list>
</property>
</bean>
<!--会查找类路径下的映射器并自动将它们创建成mapperfactorybean -->
<bean class="org.mybatis.spring.mapper.mapperscannerconfigurer"> 
<property name="sqlsessionfactorybeanname" value="mysqlsqlsessionfactory"></property>
<!-- 为映射器接口文件设置基本的包路径 --> 
<property name="basepackage" value="com.springmvc.dao" />
<!-- 该属性起到一个过滤的作用,设置该属性,那么mybatis的dao接口 只有包含该注解 才会被扫描-->
<property name="annotationclass" value="com.springmvc.base.jybatis"/>
</bean>

5) 自定义的jybatis

/**
* 标识mybatis的dao,方便{@link org.mybatis.spring.mapper.mapperscannerconfigurer}的扫描�??
* 
* 总的来说就是 target(接口) retention(java-class后依旧可用) document(包含在javadoc中) component(spring扫描)
*/
@retention(retentionpolicy.runtime) //注解的生命周期 这个是最长的 jvm加载class文件之后,仍然存在
@target(elementtype.type) //注解修改目标 (这是个接口) 接口、类、枚举、注解
@documented //该注解将被包含在javadoc中
@component //@component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。 
public @interface jybatis {
  string value() default "";
}

 6) 数据库连接参数 (这个根据自己本地的库的名字和端口 来自己写)

db.username=root
db.password=123456
db.url=jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf-8
db.dirverclass=com.mysql.jdbc.driver

这样mybatis就整合到springmvc中了,下面做一个例子,往mysql中插入一条数据 

1) 首先是jsp页面 

 还在login.jsp中写一个form

<form action="spring/student/testcontroller" method="post">
  <br />用户名: <input type="text" name="name"> <br />
  <br />年龄: <input type="text" name="age"> <br />
  <br /> 老师: <input type="text" name="teacher">
  <br /> <input type="submit" value="登录">
</form>

2) model类    然后写一个student model类

//alias是mybatis给当前model类起的别名 typealias 
@alias("student")
public class student {
private int id;
private string name;
private int age;
private string teacher;

3)studentcontroller类

@controller
@requestmapping("/spring/student")
public class studentcontroller {
@resource 
private studentservice ss;
@requestmapping(value="/testcontroller")
public string topage(student s){
system.out.println(s.tostring());
s.setid(33);
ss.save(s);
return "success";
}
}

 4) studentservice    studentserviceimpl  studentdao

public interface studentservice {
public void save(student student);
}
 //studentserviceimpl 这里要加上注解
@service("studentservice") 
public class studentserviceimpl implements studentservice {
@autowired
private studentdao studentdao;
@override
public void save(student student) {
studentdao.insert(student);
}

 studentdao  要加上自定义注解  这里spring会自动为其创建bean

@jybatis
public interface studentdao {
public void insert(student student);
}

 5)  最后是mybatis的xml文件 studentmapper.xml

<?xml version="1.0" encoding="utf-8"?>
<!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.springmvc.dao.studentdao">
<!-- com.jy.entity.system.account.account -->
<!-- com.jy.entity.oa.leave.leave -->
<resultmap id="base" type="student" > </resultmap>
<select id="find" resultmap="base" parametertype="student">
  select t.* from user1 t where 1=1 
<if test="id != null and id!='' ">
  and t.id=#{id}
</if>
</select> 
<select id="count" resulttype="int" parametertype="student">
  select count(*) from user1 t where 1=1 
</select> 
<insert id="insert" parametertype="student"> 
<![cdata[
insert into user1(
  id,
  age,
  name,
  teacher
  ) values (
  #{id},
  #{age},
  #{name},
  #{teacher}
  )
]]> 
</insert>
<update id="updateuserassetinfo" parametertype="map">
  update user1 
  set 
  id=#{id},
  age=#{age},
  name=#{name},
  teacher=#{teacher}
  where id=#{id}
</update>
</mapper>

总结

以上所述是小编给大家介绍的如何将mybatis配置到springmvc中,希望对大家有所帮助

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网