溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何將mybatis配置到springmvc中

發(fā)布時(shí)間:2020-09-03 15:10:35 來源:腳本之家 閱讀:146 作者:丁一江 欄目:編程語(yǔ)言

MyBatis簡(jiǎn)介

MyBatis 是一款優(yōu)秀的持久層框架,它支持定制化 SQL、存儲(chǔ)過程以及高級(jí)映射。MyBatis 避免了幾乎所有的 JDBC 代碼和手動(dòng)設(shè)置參數(shù)以及獲取結(jié)果集。MyBatis 可以使用簡(jiǎn)單的 XML 或注解來配置和映射原生信息,將接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java對(duì)象)映射成數(shù)據(jù)庫(kù)中的記錄。

為了更方便的連接數(shù)據(jù)庫(kù),將mybatis配置到springMVC中

1).  首先是jar包    多了3個(gè)jar  druid 這個(gè)是阿里的數(shù)據(jù)庫(kù)連接包      mybatis和 mybatis-spring 

如何將mybatis配置到springmvc中

2)  然后是項(xiàng)目目錄

如何將mybatis配置到springmvc中

3)在web.xml中 加上一個(gè)spring的配置文件

<context-param></context-param>元素含有一對(duì)參數(shù)名和參數(shù)值,用作應(yīng)用的servlet上下文初始化參數(shù)。參數(shù)名在整個(gè)Web應(yīng)用中必須是惟一的。設(shè)定web應(yīng)用的環(huán)境參數(shù)(context)

如何將mybatis配置到springmvc中

4)

  spring-mvc的內(nèi)容不變,spring-mybatis中的內(nèi)容如下

<!-- MyBatis配置 這個(gè)就是spring和mybatis的整合 也就是spring-mybatis jar-->
<bean id="mysqlSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--數(shù)據(jù)庫(kù) 多數(shù)據(jù)源配置多個(gè)-->
<property name="dataSource" ref="mysqlDataSource" />
<!-- 自動(dòng)掃描mapping.xml文件 -->
<!-- 自動(dòng)掃描entity目錄, 省掉xml里的手工配置 
該屬性可以給包中的類注冊(cè)別名,注冊(cè)后可以直接使用類名,而不用使用全限定的類名-->
<property name="typeAliasesPackage" value="com.springmvc.model" />
<!-- mysqlSqlSessionFactory會(huì)自動(dòng)掃描該路徑下的所有文件并解析。-->
<property name="mapperLocations">
<list>
<value>classpath:/mybatis/*Mapper.xml</value>
</list>
</property>
</bean>
<!--會(huì)查找類路徑下的映射器并自動(dòng)將它們創(chuàng)建成MapperFactoryBean -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
<property name="sqlSessionFactoryBeanName" value="mysqlSqlSessionFactory"></property>
<!-- 為映射器接口文件設(shè)置基本的包路徑 --> 
<property name="basePackage" value="com.springmvc.dao" />
<!-- 該屬性起到一個(gè)過濾的作用,設(shè)置該屬性,那么mybatis的dao接口 只有包含該注解 才會(huì)被掃描-->
<property name="annotationClass" value="com.springmvc.base.JYBatis"/>
</bean>

5) 自定義的JYBatis

如何將mybatis配置到springmvc中

/**
* 標(biāo)識(shí)MyBatis的DAO,方便{@link org.mybatis.spring.mapper.MapperScannerConfigurer}的掃描�??
* 
* 總的來說就是 target(接口) retention(java-class后依舊可用) document(包含在javadoc中) component(spring掃描)
*/
@Retention(RetentionPolicy.RUNTIME) //注解的生命周期 這個(gè)是最長(zhǎng)的 jvm加載class文件之后,仍然存在
@Target(ElementType.TYPE) //注解修改目標(biāo) (這是個(gè)接口) 接口、類、枚舉、注解
@Documented //該注解將被包含在javadoc中
@Component //@Component泛指組件,當(dāng)組件不好歸類的時(shí)候,我們可以使用這個(gè)注解進(jìn)行標(biāo)注。 
public @interface JYBatis {
  String value() default "";
}

 6) 數(shù)據(jù)庫(kù)連接參數(shù) (這個(gè)根據(jù)自己本地的庫(kù)的名字和端口 來自己寫)

db.username=root
db.password=123456
db.url=jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8
db.dirverClass=com.mysql.jdbc.Driver

這樣mybatis就整合到springmvc中了,下面做一個(gè)例子,往mysql中插入一條數(shù)據(jù) 

1) 首先是jsp頁(yè)面 

如何將mybatis配置到springmvc中

 還在login.jsp中寫一個(gè)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類    然后寫一個(gè)Student model類

//Alias是mybatis給當(dāng)前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會(huì)自動(dòng)為其創(chuàng)建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>

總結(jié)

以上所述是小編給大家介紹的如何將mybatis配置到springmvc中,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI