澳门新葡萄京www27111com

相关下载
文件名 文件大小
lib.rar 10m
spring_mybatis.rar 10m
使用站长秘制下载工具
本教程是为了后续学习SSM,使学习曲线更加平滑而准备的。 用于演示Spring和Mybatis如何整合在一起

步骤 1 : 基于Mybatis的课程   
步骤 2 : pojo   
步骤 3 : Mapper   
步骤 4 : Category.xml   
步骤 5 : applicationContext.xml   
步骤 6 : 测试   
步骤 7 : 可运行项目   

步骤 1 :

基于Mybatis的课程

本教程基于Mybatis的课程进行, 所以在开始之前希望你建库,表结构,Mybatis的常用用法都已经熟练了。
步骤 2 :

pojo

package com.how2java.pojo; public class Category { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Category [id=" + id + ", name=" + name + "]"; } }
步骤 3 :

Mapper

package com.how2java.mapper; import java.util.List; import com.how2java.pojo.Category; public interface CategoryMapper { public int add(Category category); public void delete(int id); public Category get(int id); public int update(Category category); public List<Category> list(); public int count(); }
步骤 4 :

Category.xml

Category.xml需要和上一个CategoryMapper类放在同一个包下,并且namespace必须写CategoryMapper

<mapper namespace="com.how2java.mapper.CategoryMapper">
Category.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.how2java.mapper.CategoryMapper"> <insert id="add" parameterType="Category" > insert into category_ ( name ) values (#{name}) </insert> <delete id="delete" parameterType="Category" > delete from category_ where id= #{id} </delete> <select id="get" parameterType="_int" resultType="Category"> select * from category_ where id= #{id} </select> <update id="update" parameterType="Category" > update category_ set name=#{name} where id=#{id} </update> <select id="list" resultType="Category"> select * from category_ </select> </mapper>
步骤 5 :

applicationContext.xml


<context:annotation-config />



<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

配置连接数据库的驱动,URL,账号和密码


<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">

扫描XML配置文件


<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

扫描Mapper类
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:annotation-config /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>admin</value> </property> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="typeAliasesPackage" value="com.how2java.pojo" /> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:com/how2java/mapper/*.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.how2java.mapper"/> </bean> </beans>
步骤 6 :

测试

使用Spring注解方式测试,拿到注入的CategoryMapper对象,当调用add方法的时候,会自动去找Category.xml里id="add"的sql语句。
package com.java.test; import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.how2java.mapper.CategoryMapper; import com.how2java.pojo.Category; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class MybatisTest { @Autowired private CategoryMapper categoryMapper; @Test public void testAdd() { Category category = new Category(); category.setName("new Category"); categoryMapper.add(category); } @Test public void testList() { System.out.println(categoryMapper); List<Category> cs=categoryMapper.list(); for (Category c : cs) { System.out.println(c.getName()); } } }
步骤 7 :

可运行项目

在右上角有本知识点对应的可运行项目下载 ,实在自己搞不出来,就下载解压出来比较一下。


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


问答区域    
2018-01-19 没有main函数吗?
cpt
rt




1 个答案

cs0sx 答案时间:2018-01-24
用的junit




答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到




2018-01-08 有用idea的小伙伴吗
Pylory



右上角直接放入idea运行,没问题。 自己重建个idea项目,导lib,照着一个一个敲,就跑不了。 报java.lang.IllegalStateException: Failed to load ApplicationContext错。 ApplicationContext.xml没问题呀,为啥加载不了呢?需要的jar包也导了,数据库连接也正常。所有src内的代码照抄、复制还是报错。

							
java.lang.IllegalStateException: Failed to load ApplicationContext

	at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:103)
	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [E:\OneDrive\Codes\Java\SSM\04SpringMybatis\out\production\04SpringMybatis\com\how2java\mapper\CategoryMapper.class]; nested exception is org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet: file [E:\Codes\Java\SSM\04SpringMybatis\out\production\04SpringMybatis\com\how2java\mapper\CategoryMapper.class]; nested exception is java.lang.IllegalArgumentException
	at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:290)
	at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:242)
	at org.mybatis.spring.mapper.ClassPathMapperScanner.doScan(ClassPathMapperScanner.java:164)
	at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.scan(ClassPathBeanDefinitionScanner.java:220)
	at org.mybatis.spring.mapper.MapperScannerConfigurer.postProcessBeanDefinitionRegistry(MapperScannerConfigurer.java:319)
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:635)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:120)
	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
	at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:102)
	at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:246)
	at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:69)
	at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:95)
	... 24 more
Caused by: org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet: file [E:\OneDrive\Codes\Java\SSM\04SpringMybatis\out\production\04SpringMybatis\com\how2java\mapper\CategoryMapper.class]; nested exception is java.lang.IllegalArgumentException
	at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:56)
	at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:96)
	at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:102)
	at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:266)
	... 36 more
Caused by: java.lang.IllegalArgumentException
	at org.springframework.asm.ClassReader.<init>(Unknown Source)
	at org.springframework.asm.ClassReader.<init>(Unknown Source)
	at org.springframework.asm.ClassReader.<init>(Unknown Source)
	at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:53)
	... 39 more






答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到





2017-12-03 mapper在这里相当dao吗?
2017-11-24 在maven项目里运行不成功
2017-10-26 怎么拿到SqlSession
2017-10-23 站长我这个tostring自动override为啥是这个样子,是不是我不小心调了什么格式了???
2017-10-23 站长我这个为啥这样???
2017-09-25 delete的parameterType="Category" ,不是应该为—int吗....为什么Category也行
2017-09-08 感觉一直对注解特别恐惧
2017-08-29 请教:applicationContext.xml配置不重复吗?
2017-08-26 原来Mybatis的主配置文件不需要了吗
2017-08-13 CategoryMapper没有找到在哪里创建的
2017-05-28 这里是把原来mybatis中测试类里创建的session放到了applicationcontext这个配置文件里了么
2017-04-22 请教:文件Category.xml和类CategoryMapper不能整合到一个内里面吗




提问之前请登陆
关于 JAVA 框架-Spring+Mybatis-整合步骤 的提问

尽量提供截图、代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 389538688
站长会在每个工作日早上尽量回答提问(如果有漏掉没有回答的,请进群提醒一下)
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
站长是玻璃心,提问的时候请语气温柔些 kiss~
截止2017-5-19日累计提问 1638个,站长回答了 1546个
截止2017-8-15日累计提问 2788个,站长回答了 2544个

上传截图