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
2) 然后是項(xiàng)目目錄
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)
4)
spring-mvc的內(nèi)容不變,spring-mybatis中的內(nèi)容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<!-- 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
1
2
3
4
5
6
7
8
9
10
11
12
|
/** * 標(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ù)的名字和端口 來自己寫)
1
2
3
4
|
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中了,下面做一個(gè)例子,往mysql中插入一條數(shù)據(jù)
1) 首先是jsp頁(yè)面
還在login.jsp中寫一個(gè)form
1
2
3
4
5
6
|
<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類
1
2
3
4
5
6
7
|
//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類
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@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
1
2
3
4
5
6
7
8
9
10
11
12
|
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
1
2
3
4
|
@jybatis public interface studentdao { public void insert(student student); } |
5) 最后是mybatis的xml文件 studentmapper.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
<?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ù)器之家網(wǎng)站的支持!
原文鏈接:http://www.cnblogs.com/studyitskill/archive/2017/11/15/7832041.html