springboot学习前的准备有那些?我是只会spring,springmvc,mybatis,

8881人阅读
mybatis-generator(2)
springboot
使用springboot+springmvc+mybatis-generator开发web程序,也算是自己参考网上的知识自己写出来的吧,里面还很多东西值得深究学习
使用IDEA和MAVEN新建springboot项目:具体怎么新建网上很多
使用maven就看一下pom.xml文件
&?xml version="1.0" encoding="UTF-8"?&
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&
&com.practice&
&1.0-SNAPSHOT&
&org.springframework.boot&
&spring-boot-starter-parent&
&1.3.6.RELEASE&
&com.practice.App&
&org.mybatis&
&org.mybatis.generator&
&mybatis-generator-core&
&org.mybatis.generator&
&mybatis-generator-maven-plugin&
&com.alibaba&
&mysql-connector-java&
&org.springframework.boot&
&spring-boot-starter-web&
&org.springframework.boot&
&spring-boot-starter-jdbc&
&com.h2database&
&org.projectlombok&
&${lombok.version}&
&provided&
&org.bgee.log4jdbc-log4j2&
&log4jdbc-log4j2-jdbc4.1&
&${log4jdbc.log4j2.version}&
&org.springframework.boot&
&spring-boot-starter-test&
&org.springframework.boot&
&spring-boot-devtools&
&1.3.6.RELEASE&
&com.jayway.restassured&
&rest-assured&
&${rest.assured.version}&
&org.springframework&
&spring-test&
&4.2.7.RELEASE&
&com.alibaba&
&fastjson&
&org.springframework.boot&
&spring-boot-autoconfigure&
&1.3.6.RELEASE&
&org.mybatis.spring.boot&
&mybatis-spring-boot-starter&
&org.mybatis&
&com.github.pagehelper&
&pagehelper&
&org.mybatis.generator&
&mybatis-generator-maven-plugin&
&mysql-connector-java&
&org.springframework.boot&
&spring-boot-maven-plugin&
&org.springframework&
&springloaded&
&${spring-loaded.version}&
修改springboot的配置文件,这里一共有四个配置文件:application.properties(默认读取)、application-dev.properties(开发环境)、application-prod.properties(生成环境)、application-test.properties(测试环境)
默认会读取resource目录下的application.properties,内容如下
spring.thymeleaf.cache=false
logging.level.jdbc=OFF
logging.level.jdbc.sqltiming=error
logging.level.jdbc.resultsettable=error
#申明使用哪个配置文件
spring.profiles.active=dev
通过spring.profiles.active=dev这个属性来控制具体使用哪个配置(dev,prod,test).
以application-dev.properties(开发环境)为例,配置如下:
#数据库配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/ssm
spring.datasource.username=root
spring.datasource.password=root
# 下面为连接池的补充设置,应用到上面所有数据源中# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.filters=stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.connectionProperties=druid.stat.mergeSql=true
# 合并多个DruidDataSource的监控数据
#spring.datasource.useGlobalDataSourceStat=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.check-template-location=true
主要是数据源的配置,这里使用druid数据源,然后通过DruidDataSourceConfig.java读取数据源的配置:
package com.practice.cfg
import com.alibaba.druid.pool.DruidDataSource
import com.github.pagehelper.PageHelper
import org.apache.ibatis.plugin.Interceptor
import org.apache.ibatis.session.SqlSessionFactory
import org.mybatis.spring.SqlSessionFactoryBean
import org.mybatis.spring.annotation.MapperScan
import org.springframework.boot.bind.RelaxedPropertyResolver
import org.springframework.context.ApplicationContextException
import org.springframework.context.EnvironmentAware
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.env.Environment
import org.springframework.core.io.support.PathMatchingResourcePatternResolver
import org.springframework.jdbc.datasource.DataSourceTransactionManager
import org.springframework.transaction.PlatformTransactionManager
import org.springframework.transaction.annotation.EnableTransactionManagement
import org.springframework.util.StringUtils
import java.sql.SQLException
import java.util.Arrays
import java.util.Properties
@Configuration
@EnableTransactionManagement
@MapperScan(value = "com.practice.mapper")
public class DruidDataSourceConfig implements EnvironmentAware {
private Environment environment
private RelaxedPropertyResolver propertyResolver
public void setEnvironment(Environment environment) {
this.environment = environment
this.propertyResolver = new RelaxedPropertyResolver(environment, "spring.datasource.")
//注册dataSource
@Bean(initMethod = "init", destroyMethod = "close")
public DruidDataSource dataSource() throws SQLException {
if (StringUtils.isEmpty(propertyResolver.getProperty("url"))) {
System.out.println("Your database connection pool configuration is incorrect!"
+ " Please check your Spring profile, current profiles are:"
+ Arrays.toString(environment.getActiveProfiles()))
throw new ApplicationContextException(
"Database connection pool is not configured correctly")
DruidDataSource druidDataSource = new DruidDataSource()
druidDataSource.setDriverClassName(propertyResolver.getProperty("driver-class-name"))
druidDataSource.setUrl(propertyResolver.getProperty("url"))
druidDataSource.setUsername(propertyResolver.getProperty("username"))
druidDataSource.setPassword(propertyResolver.getProperty("password"))
druidDataSource.setInitialSize(Integer.parseInt(propertyResolver.getProperty("initialSize")))
druidDataSource.setMinIdle(Integer.parseInt(propertyResolver.getProperty("minIdle")))
druidDataSource.setMaxActive(Integer.parseInt(propertyResolver.getProperty("maxActive")))
druidDataSource.setMaxWait(Integer.parseInt(propertyResolver.getProperty("maxWait")))
druidDataSource.setTimeBetweenEvictionRunsMillis(Long.parseLong(propertyResolver.getProperty("timeBetweenEvictionRunsMillis")))
druidDataSource.setMinEvictableIdleTimeMillis(Long.parseLong(propertyResolver.getProperty("minEvictableIdleTimeMillis")))
druidDataSource.setValidationQuery(propertyResolver.getProperty("validationQuery"))
druidDataSource.setTestWhileIdle(Boolean.parseBoolean(propertyResolver.getProperty("testWhileIdle")))
druidDataSource.setTestOnBorrow(Boolean.parseBoolean(propertyResolver.getProperty("testOnBorrow")))
druidDataSource.setTestOnReturn(Boolean.parseBoolean(propertyResolver.getProperty("testOnReturn")))
druidDataSource.setPoolPreparedStatements(Boolean.parseBoolean(propertyResolver.getProperty("poolPreparedStatements")))
druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(Integer.parseInt(propertyResolver.getProperty("maxPoolPreparedStatementPerConnectionSize")))
druidDataSource.setFilters(propertyResolver.getProperty("filters"))
return druidDataSource
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean()
sqlSessionFactoryBean.setDataSource(dataSource())
//mybatis分页
PageHelper pageHelper = new PageHelper()
Properties props = new Properties()
props.setProperty("dialect", "mysql")
props.setProperty("reasonable", "true")
props.setProperty("supportMethodsArguments", "true")
props.setProperty("returnPageInfo", "check")
props.setProperty("params", "count=countSql")
pageHelper.setProperties(props)
sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageHelper})
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver()
sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:com/practice/sqlmap/*.xml"))
return sqlSessionFactoryBean.getObject()
public PlatformTransactionManager transactionManager() throws SQLException {
return new DataSourceTransactionManager(dataSource())
这里因为后面要用到mybatis-generator和mybatise-spring集成所以需要sqlSession,这里比较重要的就是这里要指定使用的xml的位置和使用@MapperScan(value = “com.practice.mapper”)注解来扫描与xml相对应的mapper接口:
sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:com/practice/sqlmap/*.xml"))
3,使用mybatis-generator自动生成代码,在pom文件中添加了MG的插件所以这里配置一下MG的配置文件就可以,放在resource目录下:generatorConfig.properties,generatorConfig.xml。
先新建user表:
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` varchar(50) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`age` int(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
generatorConfig.xml:
&?xml version="1.0" encoding="UTF-8"?&
&!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"&
resource="generatorConfig.properties"/&
id="ssm" targetRuntime="MyBatis3"&
type="org.mybatis.generator.plugins.EqualsHashCodePlugin" /&
type="org.mybatis.generator.plugins.SerializablePlugin" /&
type="org.mybatis.generator.plugins.CaseInsensitiveLikePlugin" /&
name="suppressDate" value="true" /&
name="suppressAllComments" value="true" /&
driverClass="${driver}"
connectionURL="${url}" userId="${username}" password="${password}"&
name="forceBigDecimals" value="false" /&
targetPackage="com.practice.pojo" targetProject="src/main/java"&
name="constructorBased" value="true" /&
name="enableSubPackages" value="true" /&
name="trimStrings" value="true" /&
targetPackage="com.practice.sqlmap" targetProject="src/main/resources"&
name="enableSubPackages" value="true" /&
type="XMLMAPPER" targetPackage="com.practice.mapper"
targetProject="src/main/java"&
name="enableSubPackages" value="true" /&
schema="mybatis" tableName="user" domainObjectName="User"&
name="constructorBased" value="true" /&
name="useActualColumnNames" value="false" /&
name="ignoreQualifiersAtRuntime" value="true" /&
generatorConfig.properties:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/ssm
username=root
password=root
配置好之后双击,就会在指定目录生成代码了。
然后就是编写controller和service了:
IUserService.java:
package com.practice.
import com.practice.pojo.U
import org.
import org.springframework.stereotype.S
*: YeJunwei
Time: 10:54
public interface IUserService {
User getUserById(String id);
UserServiceImpl.java:
package com.practice.service.
import com.practice.mapper.UserM
import com.practice.pojo.U
import com.practice.service.IUserS
import org.springframework.beans.factory.annotation.A
import org.
import org.springframework.stereotype.S
*: YeJunwei
Time: 10:54
@Component
public class UserServiceImpl implements IUserService {
@Autowired
private UserMapper userM
public User getUserById(String id) {
User user=userMapper.selectByPrimaryKey(id);
UserController.java
package com.practice.controller
import com.alibaba.fastjson.JSON
import com.practice.pojo.User
import com.practice.service.IUserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.context.annotation.ComponentScan
import org.springframework.stereotype.Component
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RestController
@RestController
public class UserController {
@Autowired
private IUserService userService
@RequestMapping(value = "/user",method = RequestMethod.GET)
public String getUserById(String id){
User user=userService.getUserById(id)
String userStr= JSON.toJSONString(user)
return userStr
最后启动App.java
package com.
import org.springframework.boot.SpringA
import org.springframework.boot.autoconfigure.SpringBootA
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:41207次
排名:千里之外
原创:15篇
(1)(1)(3)(6)(2)(7)(2)(1)(1)
(window.slotbydup = window.slotbydup || []).push({
id: '4740881',
container: s,
size: '200,200',
display: 'inlay-fix'spring+boot+单点登录,spring 单点登录,springmvc 单点登录,spring boot 登录,spring boot 教学,spring boot的注解,spring boot教程,spring boot 论坛,spring boot,spring boot实战,spring boot是什么,spring boot入门,spring boot mybatis,spring boot 视频,spring boot jpa,spring boot shiro_爱问通
| 百度知道 | 百度首页 | 登录 | 注册...Spring Boot 并不是一个全新的框架,而是将已有的 ...复杂一点的同样是通过类似的 FilterRegistrationBean ......
| 百度知道 | 百度首页 | 登录 | 注册 新闻 网页 贴吧 知道 经验...首先要注意Eclipse对不同编程语言或框架都有不同的版本,比如: Spring 框架最好......
如何通过eclipse建立springMVC的简单项目,现在简单介绍...填写项目名称,点NEXT NEXT 选中Generate web.xml ...登录 63 该经验图片、文字中可能存在外站链接或电话......
| 百度知道 | 百度首页 | 登录 | 注册 新闻 网页 贴吧 知道 经验...使用Spring Boot创建Gradle工程
1 点击主菜单File-&New-&Spring Starter Project......
本经验介绍下如何用IDE创建maven项目,我用的Spring Tool Suite简称STS,因为现在大多数项目都用的是spring mvc,而STS是针对srping开发的集成工具,而且集成了maven,可以......
| 百度知道 | 百度首页 | 登录 | 注册 新闻 网页 贴吧 知道 经验...动手点一点,三步搞定一个Spring Boot的Maven工程,就问你简单不简单? 步骤阅读......
| 百度知道 | 百度首页 | 登录 | 注册 新闻 网页 贴吧 知道 经验...那么如何在MyEclipse for Spring中更改项目名呢?下面小编就来介绍一下。......
| 百度知道 | 百度首页 | 登录 | 注册...spring-2.0.6.jar Spring框架的核心包 jta.jar 会...每天早上6点
每两个小时
■ 热门推荐3005人阅读
关于学习笔记
在对java web有了一定的了解后,这两个框架没怎么写学习笔记了…毕竟项目驱动型……
关于学习资料
强烈推荐传智播客的燕青讲解的
让我对这种培训班教育的资料刮目相看(不过还是千万别去这种培训班,太傻了,以下是几个资料的地址)
如果挂了…可以留言..如果要求的人很多…我会重新上传…
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:232286次
积分:5226
积分:5226
排名:第5478名
原创:298篇
评论:47条
(5)(4)(9)(74)(4)(1)(12)(11)(2)(21)(4)(20)(29)(16)(1)(5)(2)(16)(47)(7)(2)(1)(6)(3)(1)
(window.slotbydup = window.slotbydup || []).push({
id: '4740887',
container: s,
size: '250,250',
display: 'inlay-fix'博客分类:
一、MyBatis简介与配置MyBatis+Spring+MySql
1.1MyBatis简介
MyBatis 是一个可以自定义SQL、存储过程和高级映射的持久层框架。MyBatis 摒除了大部分的JDBC代码、手工设置参数和结果集重获。MyBatis 只使用简单的XML 和注解来配置和映射基本数据类型、Map 接口和POJO 到数据库记录。相对Hibernate和Apache OJB等“一站式”ORM解决方案而言,Mybatis 是一种“半自动化”的ORM实现。需要使用的Jar包:mybatis-3.0.2.jar(mybatis核心包)。mybatis-spring-1.0.0.jar(与Spring结合包)。
下载地址:
1.2MyBatis+Spring+MySql简单配置
1.2.1搭建Spring环境
1,建立maven的web项目;2,加入Spring框架、配置文件;3,在pom.xml中加入所需要的jar包(spring框架的、mybatis、mybatis-spring、junit等);4,更改web.xml和spring的配置文件;5,添加一个jsp页面和对应的Controller;6,测试。
可参照:。
1.2.2建立MySql数据库
建立一个学生选课管理数据库。表:学生表、班级表、教师表、课程表、学生选课表。逻辑关系:每个学生有一个班级;每个班级对应一个班主任教师;每个教师只能当一个班的班主任;
使用下面的sql进行建数据库,先建立学生表,插入数据(2条以上)。
更多sql请下载项目源文件,在resource/sql中。
/* 建立数据库 */
CREATE DATABASE STUDENT_MANAGER;
USE STUDENT_MANAGER;
/***** 建立student表 *****/
CREATE TABLE STUDENT_TBL
STUDENT_ID
VARCHAR(255) PRIMARY KEY,
STUDENT_NAME
VARCHAR(10) NOT NULL,
STUDENT_SEX
VARCHAR(10),
STUDENT_BIRTHDAY
VARCHAR(255)
/*插入学生数据*/
INSERT INTO STUDENT_TBL (STUDENT_ID,
STUDENT_NAME,
STUDENT_SEX,
STUDENT_BIRTHDAY,
创建连接MySql使用的配置文件mysql.properties。
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/student_manager?user=root&password=limingnihao&useUnicode=true&characterEncoding=UTF-8
1.2.3搭建MyBatis环境
顺序随便,现在的顺序是因为可以尽量的少的修改写好的文件。
1.2.3.1创建实体类: StudentEntity
public class StudentEntity implements Serializable {
private static final long serialVersionUID = 3606831L;
private ClassEntity classE
private Date studentB
private String studentID;
private String studentN
private String studentS
public ClassEntity getClassEntity() {
return classE
public Date getStudentBirthday() {
return studentB
public String getStudentID() {
return studentID;
public String getStudentName() {
return studentN
public String getStudentSex() {
return studentS
public void setClassEntity(ClassEntity classEntity) {
this.classEntity = classE
public void setStudentBirthday(Date studentBirthday) {
this.studentBirthday = studentB
public void setStudentID(String studentID) {
this.studentID = studentID;
public void setStudentName(String studentName) {
this.studentName = studentN
public void setStudentSex(String studentSex) {
this.studentSex = studentS
1.2.3.2创建数据访问接口
Student类对应的dao接口:StudentMapper。
public interface StudentMapper {
public StudentEntity getStudent(String studentID);
public StudentEntity getStudentAndClass(String studentID);
public List&StudentEntity& getStudentAll();
public void insertStudent(StudentEntity entity);
public void deleteStudent(StudentEntity entity);
public void updateStudent(StudentEntity entity);
1.2.3.3创建SQL映射语句文件
Student类的sql语句文件StudentMapper.xmlresultMap标签:表字段与属性的映射。Select标签:查询sql。
&?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.manager.data.StudentMapper"&
&resultMap type="StudentEntity" id="studentResultMap"&
&id property="studentID" column="STUDENT_ID"/&
&result property="studentName" column="STUDENT_NAME"/&
&result property="studentSex" column="STUDENT_SEX"/&
&result property="studentBirthday" column="STUDENT_BIRTHDAY"/&
&/resultMap&
&!-- 查询学生,根据id --&
&select id="getStudent" parameterType="String" resultType="StudentEntity" resultMap="studentResultMap"&
SELECT * from STUDENT_TBL ST
WHERE ST.STUDENT_ID = #{studentID}
&!-- 查询学生列表 --&
&select id="getStudentAll"
resultType="com.manager.data.model.StudentEntity" resultMap="studentResultMap"&
SELECT * from STUDENT_TBL
1.2.3.4创建MyBatis的mapper配置文件
在src/main/resource中创建MyBatis配置文件:mybatis-config.xml。typeAliases标签:给类起一个别名。com.manager.data.model.StudentEntity类,可以使用StudentEntity代替。Mappers标签:加载MyBatis中实体类的SQL映射语句文件。
&?xml version="1.0" encoding="UTF-8" ?&
&!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"&
&configuration&
&typeAliases&
&typeAlias alias="StudentEntity" type="com.manager.data.model.StudentEntity"/&
&/typeAliases&
&mapper resource="com/manager/data/maps/StudentMapper.xml" /&
&/mappers&
&/configuration&
1.2.3.5修改Spring 的配置文件
主要是添加SqlSession的制作工厂类的bean:SqlSessionFactoryBean,(在mybatis.spring包中)。需要指定配置文件位置和dataSource。和数据访问接口对应的实现bean。通过MapperFactoryBean创建出来。需要执行接口类全称和SqlSession工厂bean的引用。
&!-- 导入属性配置文件 --&
&context:property-placeholder location="classpath:mysql.properties" /&
&bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&
&property name="driverClassName" value="${jdbc.driverClassName}" /&
&property name="url" value="${jdbc.url}" /&
&bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&
&property name="dataSource" ref="dataSource" /&
&bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"&
&property name="configLocation" value="classpath:mybatis-config.xml" /&
&property name="dataSource" ref="dataSource" /&
&!— mapper bean --&
&bean id="studentMapper" class="org.mybatis.spring.MapperFactoryBean"&
&property name="mapperInterface" value="com.manager.data.StudentMapper" /&
&property name="sqlSessionFactory" ref="sqlSessionFactory" /&
也可以不定义mapper的bean,使用注解:
将StudentMapper加入注解
@Repository
@Transactional
public interface StudentMapper {
对应的需要在dispatcher-servlet.xml中加入扫描:
&bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"&
&property name="annotationClass" value="org.springframework.stereotype.Repository"/&
&property name="basePackage" value="com.liming.manager"/&
&property name="sqlSessionFactory" ref="sqlSessionFactory"/&
1.2.4测试StudentMapper
使用SpringMVC测试,创建一个TestController,配置tomcat,访问index.do页面进行测试:
@Controller
public class TestController {
@Autowired
private StudentMapper studentM
@RequestMapping(value = "index.do")
public void indexPage() {
StudentEntity entity = studentMapper.getStudent("");
System.out.println("name:" + entity.getStudentName());
使用Junit测试:
使用Junit测试:
@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "test-servlet.xml")
public class StudentMapperTest {
@Autowired
private ClassMapper classM
@Autowired
private StudentMapper studentM
@Transactional
public void getStudentTest(){
StudentEntity entity = studentMapper.getStudent("");
System.out.println("" + entity.getStudentID() + entity.getStudentName());
List&StudentEntity& studentList = studentMapper.getStudentAll();
for( StudentEntity entityTemp : studentList){
System.out.println(entityTemp.getStudentName());
62 顶11 踩
浏览 287263
&bean id="studentMapper" class="org.mybatis.spring.MapperFactoryBean"&& &&& &property name="mapperInterface" value="com.manager.data.StudentMapper" /&& &&& &property name="sqlSessionFactory" ref="sqlSessionFactory" /&& MapperFactoryBean的路径写错了,应该是org.mybatis.spring.mapper.MapperFactoryBean
hao_evolution 写道为啥加两次 @Transactional ,在service层加不就可以了?你说的两次,第二次指的是在junit里边吧。在junit里面加@Transactional可以使在测试增删改后进行数据回滚,保证数据和测试之前的一样,不加也没问题。@Repository& @Transactional& public interface StudentMapper {& } 个人觉得这里没必要加事务,在service层去加事务控制
为啥加两次 @Transactional ,在service层加不就可以了?你说的两次,第二次指的是在junit里边吧。在junit里面加@Transactional可以使在测试增删改后进行数据回滚,保证数据和测试之前的一样,不加也没问题。
xiaojianhx 写道事务怎么加?哦,写错了.....是"事务怎么处理"使用Spring的@Transactional注解,一般写在Service层的接口方法上。
事务怎么加?哦,写错了.....是"事务怎么处理"
xiaojianhx 写道我eclipse 启动tomcat的时候报错,怎么回事啊
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [applicationContext.xml]: Initializ nested exception is java.lang.reflect.MalformedParameterizedTypeException
&bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"&
&property name="configLocation"&
&value&classpath:mybatis-config.xml&/value&
&/property&
&property name="dataSource"&
&ref bean="dataSource" /&
&/property&
遇到相同的问题,调试了一晚上没有找到原因,不知7楼是否已经解决该问题了?若解决了,请不吝赐教!
终于弄明白了,原来是spring版本的问题,MyBatis-Spring只能在spring3.0版本上使用,如果用spring3.0以下版本就会出现该问题,建议将spring升级为3.0或改用ibatis2.0!
我eclipse 启动tomcat的时候报错,怎么回事啊
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [applicationContext.xml]: Initializ nested exception is java.lang.reflect.MalformedParameterizedTypeException
&bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"&
&property name="configLocation"&
&value&classpath:mybatis-config.xml&/value&
&/property&
&property name="dataSource"&
&ref bean="dataSource" /&
&/property&
遇到相同的问题,调试了一晚上没有找到原因,不知7楼是否已经解决该问题了?若解决了,请不吝赐教!
不好意思,你的typeAliases,在哪里用到了....
在SQL映射语句文件中,使用的实体类(Java类型声明时),如果没有定义这个&typeAliases&,那么我们用到实体类都需要写带包名的全称:com.manager.data.model.StudentEntity。而使用typeAliases就给这个类起了一个名字,也就是简称:StudentEntity,所以以后在使用这个实体类的时候,我们只需要写StudentEntity,而不用写带包名的全称。
& 上一页 1
limingnihao
浏览: 1540403 次
来自: 北京
好详细,写的真全面
哪里有源代码啊,?能否发一份?还有就是 ClassEntity ...
classentity是啥?哪个包的类啊?这教程并不完整啊!
shift+alt+a
严重: Exception starting filt ...}

我要回帖

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信