如何將mybatis配置到springmvc中
MyBatis簡介
MyBatis 是一款優(yōu)秀的持久層框架,它支持定制化 SQL、存儲過程以及高級映射。MyBatis 避免了幾乎所有的 JDBC 代碼和手動設(shè)置參數(shù)以及獲取結(jié)果集。MyBatis 可以使用簡單的 XML 或注解來配置和映射原生信息,將接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java對象)映射成數(shù)據(jù)庫中的記錄。
為了更方便的連接數(shù)據(jù)庫,將mybatis配置到springMVC中
1). 首先是jar包 多了3個jar druid 這個是阿里的數(shù)據(jù)庫連接包 mybatis和 mybatis-spring

2) 然后是項目目錄

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

4)
spring-mvc的內(nèi)容不變,spring-mybatis中的內(nèi)容如下
<!-- MyBatis配置 這個就是spring和mybatis的整合 也就是spring-mybatis jar--> <bean id="mysqlSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--數(shù)據(jù)庫 多數(shù)據(jù)源配置多個--> <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> <!--會查找類路徑下的映射器并自動將它們創(chuàng)建成MapperFactoryBean --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="mysqlSqlSessionFactory"></property> <!-- 為映射器接口文件設(shè)置基本的包路徑 --> <property name="basePackage" value="com.springmvc.dao" /> <!-- 該屬性起到一個過濾的作用,設(shè)置該屬性,那么mybatis的dao接口 只有包含該注解 才會被掃描--> <property name="annotationClass" value="com.springmvc.base.JYBatis"/> </bean>
5) 自定義的JYBatis

/**
* 標(biāo)識MyBatis的DAO,方便{@link org.mybatis.spring.mapper.MapperScannerConfigurer}的掃描�??
*
* 總的來說就是 target(接口) retention(java-class后依舊可用) document(包含在javadoc中) component(spring掃描)
*/
@Retention(RetentionPolicy.RUNTIME) //注解的生命周期 這個是最長的 jvm加載class文件之后,仍然存在
@Target(ElementType.TYPE) //注解修改目標(biāo) (這是個接口) 接口、類、枚舉、注解
@Documented //該注解將被包含在javadoc中
@Component //@Component泛指組件,當(dāng)組件不好歸類的時候,我們可以使用這個注解進(jìn)行標(biāo)注。
public @interface JYBatis {
String value() default "";
}
6) 數(shù)據(jù)庫連接參數(shù) (這個根據(jù)自己本地的庫的名字和端口 來自己寫)
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中插入一條數(shù)據(jù)
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給當(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會自動為其創(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中,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- 基于SpringBoot與Mybatis實現(xiàn)SpringMVC Web項目
- SpringMvc+Mybatis+Pagehelper分頁詳解
- SpringMVC與Mybatis集合實現(xiàn)調(diào)用存儲過程、事務(wù)控制實例
- springmvc與mybatis集成配置實例詳解
- MyBatis與SpringMVC相結(jié)合實現(xiàn)文件上傳、下載功能
- SpringMVC整合mybatis實例代碼
- AngularJS整合Springmvc、Spring、Mybatis搭建開發(fā)環(huán)境
- 解決springmvc+mybatis+mysql中文亂碼問題
- Java簡單實現(xiàn)SpringMVC+MyBatis分頁插件
- SpringMVC+MyBatis聲明式事務(wù)管理
相關(guān)文章
Java利用反射動態(tài)設(shè)置對象字段值的實現(xiàn)
橋梁信息維護(hù)需要做到字段級別的權(quán)限控制,本文主要介紹了Java利用反射動態(tài)設(shè)置對象字段值的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-01-01
Java獲取XML節(jié)點總結(jié)之讀取XML文檔節(jié)點的方法
下面小編就為大家?guī)硪黄狫ava獲取XML節(jié)點總結(jié)之讀取XML文檔節(jié)點的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10
一文帶你掌握J(rèn)ava8中Lambda表達(dá)式 函數(shù)式接口及方法構(gòu)造器數(shù)組的引用
Java 8 (又稱為 jdk 1.8) 是 Java 語言開發(fā)的一個主要版本。 Oracle 公司于 2014 年 3 月 18 日發(fā)布 Java 8 ,它支持函數(shù)式編程,新的 JavaScript 引擎,新的日期 API,新的Stream API 等2021-10-10
Java+Ajax實現(xiàn)的用戶名重復(fù)檢驗功能實例詳解
這篇文章主要介紹了Java+Ajax實現(xiàn)的用戶名重復(fù)檢驗功能,結(jié)合實例形式詳細(xì)分析了java針對用戶名提交的ajax數(shù)據(jù)庫查詢與重復(fù)檢查功能相關(guān)實現(xiàn)技巧與操作注意事項,需要的朋友可以參考下2018-12-12
基于java web獲取網(wǎng)頁訪問次數(shù)代碼實例
這篇文章主要介紹了基于java web獲取網(wǎng)頁訪問次數(shù)代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02

