mybatis + postgresql 遇到的问题
标签:
小编还为您整理了以下内容,可能对您也有帮助:
解决Mybatis在postgresql查询数组无法映射到实体类中的问题
ArrayListTypeHandler
JSONTypeHandler
ListTypeHandler
MybatisMetaHandler
MybatisPlusConfig
配置好映射放在配置文件下的目录中
mybatis-plus:
global-config:
db-config:
id-type: auto
type-handlers-package: com.xx.xx.xx.xx.plugin
mybatis兑现postgresql数据库的分页查询怎么解决
<mapper namespace="com.jim.logstashmvc.dao.generated.mapper.ProductMapper"> <resultMap id="BaseResultMap" type="com.jim.logstashmvc.dao.generated.entity.Product"> <!-- WARNING - @mbggenerated --> <id column="id" jdbcType="BIGINT" property="id" /> <result column="name" jdbcType="VARCHAR" property="name" /> </resultMap></mapper>
public interface ProductMapper extends Mapper<Product> {}
如何与spring集成?
<!-- MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- Spring集成 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <!-- MBG --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>${MBG.version}</version> <scope>compile</scope> <optional>true</optional> </dependency> <!-- 分页 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper.version}</version> </dependency> <!-- 通用Mapper --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>${mapper.version}</version> </dependency> <!-- TkMybatis 会使用到JPA的注解 --> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.3-1102-jdbc41</version> </dependency>
<!--MBG--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>${MBG.version}</version> <configuration> <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> <dependencies> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.3-1102-jdbc41</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>${mapper.version}</version> </dependency> </dependencies> </plugin>
注意下targetRuntime,这里采用的是MyBatis3Simple,它的默认选项是MyBatis3。如果采用通用mapper,我们在spring扫描接口时可以这样写。
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="jimSqlSessionFactory"/> <property name="basePackage" value="com.jim.logstashmvc.dao.generated.mapper"/> </bean>
如果是MyBatis3,生成的mapper.xml格式会复杂很多,我之前遇到过这样的问题:使用MyBatis3生成的mapper.xml然后错误 的配置了MapperScannerConfigurer为下面通用mapper模式,提示我的错误如下,原因可以认定是配置问题(不是某个mapper.xml中的id重复问题) ,后续再研究下非通用mapper的配置。
Caused by: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.jim.logstashmvc.dao.generated.mapper.ProductMapper.selectByExampleat org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:837)at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:809)
上面的报错信息已经找到,原因是因为采用了MyBatis3方式生成Mapper,但同时在配置文件中错误的增加了通用Mapper的插件,它会在Mapper接口上增加一个Mapper<T>的继承,而一旦继承了这个通过接口,它里面包含的方法Id与MyBatis3生成的方法Id正好重复,导致了在编译时提示上面的错误。
使用了通用Mapper之后,有个缺点就是使用Example时没有强类型的方法了,比如不能这样:
ProductExample example =new ProductExample(); ProductExample.Criteria criteria=example.createCriteria(); if (product.getId() != null) { criteria.andIdEqualTo(product.getId()); }
而只能这样写,字段名称需要以字符串的形式进行指定。这种方式非常适合于动态构建查询,比如:列表页的动态条件搜索
Example example = new Example(Product.class); Example.Criteria criteria = example.createCriteria(); if (product.getId() != null) { criteria.andEqualTo("id", product.getId()); }
我们之前的项目为了能够使用动态条件构建,又想使用强类型的Example进行简单的查询,我们扩展了targetRuntime,让其生成的Mapper即继承自Map<T>又生成了具体的方法,只不过生成的具体方法在名称上做了调整,在名称中间增加了一个独有的占有符,默认MBG生成的查询方法名称是selectByExample,这里我们另外生成一个selectByConcreteExample。这样做也是有代价的,生成的mapper.xml也不再只包含实体映射,mapper接口也不再只是继承的近似空接口了,具体怎么用仁者见仁吧。
public interface ImageMapper extends PartyInterface, RowBoundsMapper<Image>, BaseMapper<Image>, ExampleMapper<Image> { int countByConcreteExample(ImageExample example); int deleteByConcreteExample(ImageExample example); List<Image> selectByConcreteExample(ImageExample example); int updateByConcreteExampleSelective(@Param("record") Image record, @Param("example") ImageExample example); int updateByConcreteExample(@Param("record") Image record, @Param("example") ImageExample example);}
生成器的配置详细如下:
<generatorConfiguration> <properties resource="config.properties"/> <context id="jim" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <plugin type="${mapper.plugin}"> <property name="mappers" value="${mapper.Mapper}"/> </plugin> <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"> </jdbcConnection> <javaModelGenerator targetPackage="${targetModelPackage}" targetProject="${targetJavaProject}"/> <sqlMapGenerator targetPackage="mapper" targetProject="${targetResourcesProject}"/> <javaClientGenerator targetPackage="${targetMapperPackage}" targetProject="${targetJavaProject}" type="XMLMAPPER"> </javaClientGenerator> <table tableName="product" domainObjectName="Product"></table> </context></generatorConfiguration>
<bean id="jimDataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClass}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="initialSize" value="5"/> <property name="minIdle" value="10"/> <property name="maxWait" value="60000"/> <property name="timeBetweenEvictionRunsMillis" value="60000"/> <property name="minEvictableIdleTimeMillis" value="3600000"/> <property name="validationQuery" value="SELECT 1"/> <property name="testWhileIdle" value="true"/> <property name="testOnBorrow" value="false"/> <property name="testOnReturn" value="false"/> </bean> <bean id="jimSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="jimDataSource"/> <property name="mapperLocations" value="classpath:mapper/*.xml"/> <property name="typeAliasesPackage" value="com.jim.logstashmvc.dao.generated.entity"/> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageHelper"> <property name="properties"> <value> dialect=postgresql reasonable=true supportMethodsArguments=true returnPageInfo=check params=count=countSql </value> </property> </bean> </array> </property> </bean> <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="jimSqlSessionFactory"/> <property name="basePackage" value="com.jim.logstashmvc.dao.generated.mapper"/> </bean>
@Servicepublic interface IService<T> { T selectByKey(Object key); int save(T entity); int delete(Object key); int updateAll(T entity); int updateNotNull(T entity); List<T> selectByExample(Object example); //TODO 其他...}
public abstract class BaseService<T> implements IService<T> { @Autowired protected Mapper<T> mapper; public Mapper<T> getMapper() { return mapper; } @Override public T selectByKey(Object key) { return mapper.selectByPrimaryKey(key); } public int save(T entity) { return mapper.insert(entity); } public int delete(Object key) { return mapper.deleteByPrimaryKey(key); } public int updateAll(T entity) { return mapper.updateByPrimaryKey(entity); } public int updateNotNull(T entity) { return mapper.updateByPrimaryKeySelective(entity); } public List<T> selectByExample(Object example) { return mapper.selectByExample(example); } //TODO 其他...}
@Servicepublic class ProductServiceImpl extends BaseService<Product> implements ProductService { @Override public List<Product> selectByProduct(Product product, int page, int rows) { Example example = new Example(Product.class); Example.Criteria criteria = example.createCriteria(); if(!StringUtils.isBlank(product.getName())){ criteria.andEqualTo("name",product.getName()); } if (product.getId() != null) { criteria.andEqualTo("id", product.getId()); } PageHelper.startPage(page, rows); return selectByExample(example); }}
安装postgresql并且成功远程连接,集成MBG生成mapper以及model,然后将mybatis与spring集成,最后通过通用mapper中联起来就达到了我们的目的:通过少量的代码完成大部分的工作,重复劳动交给工具完成。但通用mapper有它的优点也就有它的缺点,需要根据项目环境来平衡,个人感觉利大于弊。
本文引用:
1:http://www.mybatis.tk/
2:https://github.com/abel533/Mybatis-Spring
mybatis+postgresql平台
标签:
mybatis兑现postgresql数据库的分页查询怎么解决
<mapper namespace="com.jim.logstashmvc.dao.generated.mapper.ProductMapper"> <resultMap id="BaseResultMap" type="com.jim.logstashmvc.dao.generated.entity.Product"> <!-- WARNING - @mbggenerated --> <id column="id" jdbcType="BIGINT" property="id" /> <result column="name" jdbcType="VARCHAR" property="name" /> </resultMap></mapper>
public interface ProductMapper extends Mapper<Product> {}
如何与spring集成?
<!-- MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- Spring集成 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <!-- MBG --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>${MBG.version}</version> <scope>compile</scope> <optional>true</optional> </dependency> <!-- 分页 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper.version}</version> </dependency> <!-- 通用Mapper --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>${mapper.version}</version> </dependency> <!-- TkMybatis 会使用到JPA的注解 --> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.3-1102-jdbc41</version> </dependency>
<!--MBG--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>${MBG.version}</version> <configuration> <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> <dependencies> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.3-1102-jdbc41</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>${mapper.version}</version> </dependency> </dependencies> </plugin>
注意下targetRuntime,这里采用的是MyBatis3Simple,它的默认选项是MyBatis3。如果采用通用mapper,我们在spring扫描接口时可以这样写。
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="jimSqlSessionFactory"/> <property name="basePackage" value="com.jim.logstashmvc.dao.generated.mapper"/> </bean>
如果是MyBatis3,生成的mapper.xml格式会复杂很多,我之前遇到过这样的问题:使用MyBatis3生成的mapper.xml然后错误 的配置了MapperScannerConfigurer为下面通用mapper模式,提示我的错误如下,原因可以认定是配置问题(不是某个mapper.xml中的id重复问题) ,后续再研究下非通用mapper的配置。
Caused by: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.jim.logstashmvc.dao.generated.mapper.ProductMapper.selectByExampleat org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:837)at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:809)
上面的报错信息已经找到,原因是因为采用了MyBatis3方式生成Mapper,但同时在配置文件中错误的增加了通用Mapper的插件,它会在Mapper接口上增加一个Mapper<T>的继承,而一旦继承了这个通过接口,它里面包含的方法Id与MyBatis3生成的方法Id正好重复,导致了在编译时提示上面的错误。
使用了通用Mapper之后,有个缺点就是使用Example时没有强类型的方法了,比如不能这样:
ProductExample example =new ProductExample(); ProductExample.Criteria criteria=example.createCriteria(); if (product.getId() != null) { criteria.andIdEqualTo(product.getId()); }
而只能这样写,字段名称需要以字符串的形式进行指定。这种方式非常适合于动态构建查询,比如:列表页的动态条件搜索
Example example = new Example(Product.class); Example.Criteria criteria = example.createCriteria(); if (product.getId() != null) { criteria.andEqualTo("id", product.getId()); }
我们之前的项目为了能够使用动态条件构建,又想使用强类型的Example进行简单的查询,我们扩展了targetRuntime,让其生成的Mapper即继承自Map<T>又生成了具体的方法,只不过生成的具体方法在名称上做了调整,在名称中间增加了一个独有的占有符,默认MBG生成的查询方法名称是selectByExample,这里我们另外生成一个selectByConcreteExample。这样做也是有代价的,生成的mapper.xml也不再只包含实体映射,mapper接口也不再只是继承的近似空接口了,具体怎么用仁者见仁吧。
public interface ImageMapper extends PartyInterface, RowBoundsMapper<Image>, BaseMapper<Image>, ExampleMapper<Image> { int countByConcreteExample(ImageExample example); int deleteByConcreteExample(ImageExample example); List<Image> selectByConcreteExample(ImageExample example); int updateByConcreteExampleSelective(@Param("record") Image record, @Param("example") ImageExample example); int updateByConcreteExample(@Param("record") Image record, @Param("example") ImageExample example);}
生成器的配置详细如下:
<generatorConfiguration> <properties resource="config.properties"/> <context id="jim" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <plugin type="${mapper.plugin}"> <property name="mappers" value="${mapper.Mapper}"/> </plugin> <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"> </jdbcConnection> <javaModelGenerator targetPackage="${targetModelPackage}" targetProject="${targetJavaProject}"/> <sqlMapGenerator targetPackage="mapper" targetProject="${targetResourcesProject}"/> <javaClientGenerator targetPackage="${targetMapperPackage}" targetProject="${targetJavaProject}" type="XMLMAPPER"> </javaClientGenerator> <table tableName="product" domainObjectName="Product"></table> </context></generatorConfiguration>
<bean id="jimDataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClass}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="initialSize" value="5"/> <property name="minIdle" value="10"/> <property name="maxWait" value="60000"/> <property name="timeBetweenEvictionRunsMillis" value="60000"/> <property name="minEvictableIdleTimeMillis" value="3600000"/> <property name="validationQuery" value="SELECT 1"/> <property name="testWhileIdle" value="true"/> <property name="testOnBorrow" value="false"/> <property name="testOnReturn" value="false"/> </bean> <bean id="jimSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="jimDataSource"/> <property name="mapperLocations" value="classpath:mapper/*.xml"/> <property name="typeAliasesPackage" value="com.jim.logstashmvc.dao.generated.entity"/> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageHelper"> <property name="properties"> <value> dialect=postgresql reasonable=true supportMethodsArguments=true returnPageInfo=check params=count=countSql </value> </property> </bean> </array> </property> </bean> <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="jimSqlSessionFactory"/> <property name="basePackage" value="com.jim.logstashmvc.dao.generated.mapper"/> </bean>
@Servicepublic interface IService<T> { T selectByKey(Object key); int save(T entity); int delete(Object key); int updateAll(T entity); int updateNotNull(T entity); List<T> selectByExample(Object example); //TODO 其他...}
public abstract class BaseService<T> implements IService<T> { @Autowired protected Mapper<T> mapper; public Mapper<T> getMapper() { return mapper; } @Override public T selectByKey(Object key) { return mapper.selectByPrimaryKey(key); } public int save(T entity) { return mapper.insert(entity); } public int delete(Object key) { return mapper.deleteByPrimaryKey(key); } public int updateAll(T entity) { return mapper.updateByPrimaryKey(entity); } public int updateNotNull(T entity) { return mapper.updateByPrimaryKeySelective(entity); } public List<T> selectByExample(Object example) { return mapper.selectByExample(example); } //TODO 其他...}
@Servicepublic class ProductServiceImpl extends BaseService<Product> implements ProductService { @Override public List<Product> selectByProduct(Product product, int page, int rows) { Example example = new Example(Product.class); Example.Criteria criteria = example.createCriteria(); if(!StringUtils.isBlank(product.getName())){ criteria.andEqualTo("name",product.getName()); } if (product.getId() != null) { criteria.andEqualTo("id", product.getId()); } PageHelper.startPage(page, rows); return selectByExample(example); }}
安装postgresql并且成功远程连接,集成MBG生成mapper以及model,然后将mybatis与spring集成,最后通过通用mapper中联起来就达到了我们的目的:通过少量的代码完成大部分的工作,重复劳动交给工具完成。但通用mapper有它的优点也就有它的缺点,需要根据项目环境来平衡,个人感觉利大于弊。
本文引用:
1:http://www.mybatis.tk/
2:https://github.com/abel533/Mybatis-Spring
mybatis+postgresql平台
标签:
怎样使用Mybatis处理PostgreSQL的jsonb类型的数据
ostgreSQL 9.4 正在加载一项新功能叫jsonb,是一种新型资料,可以储存支援GIN索引的JSON 资料。换言之,此功能,在即将来临的更新中最重要的是,如果连这都不重要的话,那就把Postgres 置于文件为本数据库系统的推荐位置吧。
自从9.2开始,一个整合JSON 资料类型已经存在,带有一整套功能(例如资料产生和资料解构功能),还有9.3新增的操作者。当使用JSON 资料类型,资料的被存储成一完全一样的副本,功能还在此之上运作,还另外需要后台运作的重新分析。
这心得JSONB 资料类型以已降解的2元格式存储,所以,插入此资料会比JSON高效,因为后台不再需要重新分析,因此让它更快速运行,而且还兼顾GIN 索引。就是因为最后这个原因,我们实际上建议读者使用jsonb来代替json制作程式(当然你还可以因应需要而使用json)。请记住jsonb使用相同的操作者和功能,读者们可以看我之前的帖子去令你得到些什么启发(或者干脆看Postgres的文件)。
怎样使用Mybatis处理PostgreSQL的jsonb类型的数据
ostgreSQL 9.4 正在加载一项新功能叫jsonb,是一种新型资料,可以储存支援GIN索引的JSON 资料。换言之,此功能,在即将来临的更新中最重要的是,如果连这都不重要的话,那就把Postgres 置于文件为本数据库系统的推荐位置吧。
自从9.2开始,一个整合JSON 资料类型已经存在,带有一整套功能(例如资料产生和资料解构功能),还有9.3新增的操作者。当使用JSON 资料类型,资料的被存储成一完全一样的副本,功能还在此之上运作,还另外需要后台运作的重新分析。
这心得JSONB 资料类型以已降解的2元格式存储,所以,插入此资料会比JSON高效,因为后台不再需要重新分析,因此让它更快速运行,而且还兼顾GIN 索引。就是因为最后这个原因,我们实际上建议读者使用jsonb来代替json制作程式(当然你还可以因应需要而使用json)。请记住jsonb使用相同的操作者和功能,读者们可以看我之前的帖子去令你得到些什么启发(或者干脆看Postgres的文件)。
数据库mysql创建表格老是出错,看不懂英文提示?
来自:51CTO(作者:superZS)
我在刚开始学习数据库的时候,没少走弯路。经常会遇到各种稀奇古怪的 error 信息,遇到报错会很慌张,急需一个解决问题的办法。跟无头苍蝇一样,会不加思索地把错误粘到百度上,希望赶紧查找一下有没有好的处理问题的方法。我想这个应该是刚从事数据库的小白,都会遇到窘境。
今天就给大家列举 MySQL 数据库中,最经典的十大错误案例,并附有处理问题的解决思路和方法,希望能给刚入行,或数据库爱好者一些帮助,今后再遇到任何报错,我们都可以很淡定地去处理。
学习任何一门技术的同时,其实就是自我*的过程。沉下心,尝试去拥抱数据的世界!
Top 1:
Too many connections(连接数过多,导致连接不上数据库,业务无法正常进行)
问题还原
解决问题的思路:
1、首先先要考虑在我们 MySQL 数据库参数文件里面,对应的 max_connections 这个参数值是不是设置的太小了,导致客户端连接数超过了数据库所承受的最大值。
● 该值默认大小是151,我们可以根据实际情况进行调整。
● 对应解决办法:set global max_connections=500
但这样调整会有隐患,因为我们无法确认数据库是否可以承担这么大的连接压力,就好比原来一个人只能吃一个馒头,但现在却非要让他吃 10 个,他肯定接受不了。反应到服务器上面,就有可能会出现宕机的可能。
所以这又反应出了,我们在新上线一个业务系统的时候,要做好压力测试。保证后期对数据库进行优化调整。
2、其次可以* Innodb 的并发处理数量,如果 innodb_thread_concurrency = 0(这种代表不受*) 可以先改成 16或是64 看服务器压力。如果非常大,可以先改的小一点让服务器的压力下来之后,然后再慢慢增大,根据自己的业务而定。个人建议可以先调整为 16 即可。
MySQL 随着连接数的增加性能是会下降的,可以让开发配合设置 thread pool,连接复用。在MySQL商业版中加入了thread pool这项功能
另外对于有的监控程序会读取 information_schema 下面的表,可以考虑关闭下面的参数
innodb_stats_on_metadata=0
set global innodb_stats_on_metadata=0
Top 2:(主从复制报错类型)
Last_SQL_Errno: 1062 (从库与主库数据冲突)
Last_Errno: 1062
Last_Error: Could not execute Write_rows event on table test.t;
Duplicate entry '4' for key 'PRIMARY',
Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY;
the event's master log mysql-bin.000014, end_log_pos 1505
针对这个报错,我们首先要考虑是不是在从库中误操作导致的。结果发现,我们在从库中进行了一条针对有主键表的 sql 语句的插入,导致主库再插入相同 sql 的时候,主从状态出现异常。发生主键冲突的报错。
解决方法:
在确保主从数据一致性的前提下,可以在从库进行错误跳过。一般使用 percona-toolkit 中的 pt-slave-restart 进行。
在从库完成如下操作
[root@zs bin]# ./pt-slave-restart -uroot -proot123
2017-07-20T14:05:30 p=...,u=root node4-relay-bin.000002 1506 1062
之后最好在从库中开启 read_only 参数,禁止在从库进行写入操作
Last_IO_Errno: 1593(server-id冲突)
Last_IO_Error:
Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids;
these ids must be different for replication to work
(or the --replicate-same-server-id option must be used on slave but this
does not always make sense; please check the manual before using it)
这个报错出现之后,就看一目了然看到两台机器的 server-id 是一样的。
在搭建主从复制的过程中,我们要确保两台机器的 server-id 是唯一的。这里再强调一下 server-id 的命名规则(服务器 ip 地址的最后一位+本 MySQL 服务的端口号)
解决方法:
在主从两台机器上设置不同的 server-id。
Last_SQL_Errno: 1032(从库少数据,主库更新的时候,从库报错)
Last_SQL_Error:
Could not execute Update_rows event on table test.t; Can't find record
in 't', Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the
event's master log mysql-bin.000014, end_log_pos 1708
解决问题的办法:
根据报错信息,我们可以获取到报错日志和position号,然后就能找到主库执行的哪条sql,导致的主从报错。
在主库执行:
/usr/local/mysql/bin/mysqlbinlog --no-defaults -v -v --base64-output=decode-rows /data/mysql/mysql-bin.000014 |grep -A 10 1708 > 1.log
cat 1.log
#170720 14:20:15 server id 3 end_log_pos 1708 CRC32 0x97b6bdec Update_rows: table id 113 flags: STMT_END_F
### UPDATE `test`.`t`
### WHERE
### @1=4 /* INT meta=0 nullable=0 is_null=0 */
### @2='dd' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
### SET
### @1=4 /* INT meta=0 nullable=0 is_null=0 */
### @2='ddd' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
# at 1708
#170720 14:20:15 server id 3 end_log_pos 1739 CRC32 0xecaf1922 Xid = 654
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
获取到 sql 语句之后,就可以在从库反向执行 sql 语句。把从库缺少的 sql 语句补全,解决报错信息。
在从库依次执行:
mysql> insert into t (b) values ('ddd');
Query OK, 1 row affected (0.01 sec)
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[root@node4 bin]# ./pt-slave-restart -uroot -proot123
2017-07-20T14:31:37 p=...,u=root node4-relay-bin.000005 283 1032
Top 3:MySQL安装过程中的报错
[root@zs data]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &[1] 3758
[root@zs data]# 170720 14:41:24 mysqld_safe Logging to '/data/mysql/error.log'.
170720 14:41:24 mysqld_safe Starting mysqld daemon with databases from /data/mysql170720
14:41:25 mysqld_safe mysqld from pid file /data/mysql/node4.pid ended
170720 14:41:24 mysqld_safe Starting mysqld daemon with databases from /data/mysql2017-07-20
14:41:25 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated.
Please use --explicit_defaults_for_timestamp server option
(see documentation for more details)./usr/local/mysql/bin/mysqld:
File '/data/mysql/mysql-bin.index' not found (Errcode: 13 - Permission denied)
2017-07-20 14:41:25 4388 [ERROR] Aborting
解决思路:
遇到这样的报错信息,我们要学会时时去关注错误日志 error log 里面的内容。看见了关键的报错点 Permission denied。证明当前 MySQL 数据库的数据目录没有权限。
解决方法:
[root@zs data]# chown mysql:mysql -R mysql
[root@zs data]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &
[1] 4402
[root@zs data]# 170720 14:45:56 mysqld_safe Logging to '/data/mysql/error.log'.
170720 14:45:56 mysqld_safe Starting mysqld daemon with databases from /data/mysql
启动成功。
如何避免这类问题,个人建议在安装 MySQL 初始化的时候,一定加上--user=mysql,这样就可以避免权限问题。
./mysql_install_db --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --defaults-file=/etc/my.cnf --user=mysql
Top 4:数据库密码忘记的问题
[root@zs ~]# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@zs ~]# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
我们有可能刚刚接手别人的 MySQL 数据库,而且没有完善的交接文档。root 密码可以丢失或者忘记了。
解决思路:
目前是进入不了数据库的情况,所以我们要考虑是不是可以跳过权限。因为在数据库中,mysql数据库中user表记录着我们用户的信息。
解决方法:
启动 MySQL 数据库的过程中,可以这样执行:
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables &
这样启动,就可以不用输入密码,直接进入 mysql 数据库了。然后在修改你自己想要改的root密码即可。
update mysql.user set password=password('root123') where user='root';
Top 5:truncate 删除数据,导致自动清空自增ID,前端返回报错 not found。
这个问题的出现,就要考虑下 truncate 和 delete 的区别了。
看下实验演练:
首先先创建一张表;
CREATE TABLE `t` (
`a` int(11) NOT NULL AUTO_INCREMENT,
`b` varchar(20) DEFAULT NULL,
PRIMARY KEY (`a`),
KEY `b` (`b`)
) ENGINE=InnoDB AUTO_INCREMENT=300 DEFAULT CHARSET=utf8
插入三条数据:
mysql> insert into t (b) values ('aa');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t (b) values ('bb');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t (b) values ('cc');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t;
+-----+------+
| a | b |
+-----+------+
| 300 | aa |
| 301 | bb |
| 302 | cc |
+-----+------+
3 rows in set (0.00 sec)
先用 delete 进行删除全表信息,再插入新值。
结果发现 truncate 把自增初始值重置了,自增属性从1开始记录了。当前端用主键id进行查询时,就会报没有这条数据的错误。
个人建议不要使用 truncate 对表进行删除操作,虽然可以回收表空间,但是会涉及自增属性问题。这些坑,我们不要轻易钻进去。
Top 6:
阿里云 MySQL 的配置文件中,需要注意一个参数设置就是:
lower_case_table_names = 0;默认情况
lower_case_table_names = 1;是不区分大小写 . 如果报你小写的表名找不到, 那你就把远端数据库的表名改成小写 , 反之亦然 . 注意 Mybatis 的 Mapper 文件的所有表名也要相应修改
Top 7:
有同学经常会问张老师,为什么我的数据库总会出现中文乱码的情况。一堆????不知道怎么回事。当向数据库中写入创建表,并插入中文时,会出现这种问题。此报错会涉及数据库字符集的问题。
解决思路:
对于中文乱码的情况,记住老师告诉你的三个统一就可以。还要知道在目前的mysql数据库中字符集编码都是默认的UTF8
处理办法:
1、数据终端,也就是我们连接数据库的工具设置为 utf8
2、操作系统层面;可以通过 cat /etc/sysconfig/i18n 查看;也要设置为 utf8
3、数据库层面;在参数文件中的 mysqld 下,加入 character-set-server=utf8。
Emoji 表情符号录入 mysql 数据库中报错。
Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x97\xF0\x9F...' for column 'CONTENT' at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2734)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1379)
解决思路:针对表情插入的问题,一定还是字符集的问题。
处理方法:我们可以直接在参数文件中,加入
vim /etc/my.cnf
[mysqld]
init-connect='SET NAMES utf8mb4'
character-set-server=utf8mb4
注:utf8mb4 是 utf8 的超集。
Top 8:使用 binlog_format=statement 这种格式,跨库操作,导致从库丢失数据,用户访问导致出现错误数据信息。
当前数据库二进制日志的格式为:binlog_format=statement
在主库设置binlog-do-db=mydb1(只同步mydb1这一个库)
在主库执行use mydb2;
insert into mydb1.t1 values ('bb');这条语句不会同步到从库。
但是这样操作就可以;
use mydb1;
insert into mydb1.t1 values ('bb');因为这是在同一个库中完成的操作。
在生产环境中建议使用binlog的格式为row,而且慎用binlog-do-db参数。
Top 9:MySQL 数据库连接超时的报错 ;
org.hibernate.util.JDBCExceptionReporter - SQL Error:0, SQLState: 08S01
org.hibernate.util.JDBCExceptionReporter - The last packet successfully received from the server was43200 milliseconds ago.The last packet sent successfully to the server was 43200 milliseconds ago, which is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection 'autoReconnect=true' to avoid this problem.
org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.exception.JDBCConnectionException: Could not execute JDBC batch update
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Connection.close() has already been called. Invalid operation in this state.
org.hibernate.util.JDBCExceptionReporter - SQL Error:0, SQLState: 08003
org.hibernate.util.JDBCExceptionReporter - No operations allowed after connection closed. Connection was implicitly closed e to underlying exception/error:
** BEGIN NESTED EXCEPTION **
大多数做 DBA 的同学,可能都会被开发人员告知,你们的数据库报了这个错误了。赶紧看看是哪里的问题。
这个问题是由两个参数影响的,wait_timeout 和 interactive_timeout。数据默认的配置时间是28800(8小时)意味着,超过这个时间之后,MySQL 数据库为了节省资源,就会在数据库端断开这个连接,Mysql服务器端将其断开了,但是我们的程序再次使用这个连接时没有做任何判断,所以就挂了。
解决思路:
先要了解这两个参数的特性;这两个参数必须同时设置,而且必须要保证值一致才可以。
我们可以适当加大这个值,8小时太长了,不适用于生产环境。因为一个连接长时间不工作,还占用我们的连接数,会消耗我们的系统资源。
解决方法:
可以适当在程序中做判断;强烈建议在操作结束时更改应用程序逻辑以正确关闭连接;然后设置一个比较合理的timeout的值(根据业务情况来判断)
Top 10 :can't open file (errno:24)
有的时候,数据库跑得好好的,突然报不能打开数据库文件的错误了。
解决思路:
首先我们要先查看数据库的 error log。然后判断是表损坏,还是权限问题。还有可能磁盘空间不足导致的不能正常访问表;操作系统的*也要关注下;用 perror 工具查看具体错误!
linux:/usr/local/mysql/bin # ./perror 24
OS error code 24: Too many open files
超出最大打开文件数*!ulimit -n查看系统的最大打开文件数是65535,不可能超出!那必然是数据库的最大打开文件数超出*!
在 MySQL 里查看最大打开文件数*命令:show variables like 'open_files_limit';
发现该数值过小,改为2048,重启 MySQL,应用正常
处理方法:
repair table ;
chown mysql权限
清理磁盘中的垃圾数据
数据库mysql创建表格老是出错,看不懂英文提示?
来自:51CTO(作者:superZS)
我在刚开始学习数据库的时候,没少走弯路。经常会遇到各种稀奇古怪的 error 信息,遇到报错会很慌张,急需一个解决问题的办法。跟无头苍蝇一样,会不加思索地把错误粘到百度上,希望赶紧查找一下有没有好的处理问题的方法。我想这个应该是刚从事数据库的小白,都会遇到窘境。
今天就给大家列举 MySQL 数据库中,最经典的十大错误案例,并附有处理问题的解决思路和方法,希望能给刚入行,或数据库爱好者一些帮助,今后再遇到任何报错,我们都可以很淡定地去处理。
学习任何一门技术的同时,其实就是自我*的过程。沉下心,尝试去拥抱数据的世界!
Top 1:
Too many connections(连接数过多,导致连接不上数据库,业务无法正常进行)
问题还原
解决问题的思路:
1、首先先要考虑在我们 MySQL 数据库参数文件里面,对应的 max_connections 这个参数值是不是设置的太小了,导致客户端连接数超过了数据库所承受的最大值。
● 该值默认大小是151,我们可以根据实际情况进行调整。
● 对应解决办法:set global max_connections=500
但这样调整会有隐患,因为我们无法确认数据库是否可以承担这么大的连接压力,就好比原来一个人只能吃一个馒头,但现在却非要让他吃 10 个,他肯定接受不了。反应到服务器上面,就有可能会出现宕机的可能。
所以这又反应出了,我们在新上线一个业务系统的时候,要做好压力测试。保证后期对数据库进行优化调整。
2、其次可以* Innodb 的并发处理数量,如果 innodb_thread_concurrency = 0(这种代表不受*) 可以先改成 16或是64 看服务器压力。如果非常大,可以先改的小一点让服务器的压力下来之后,然后再慢慢增大,根据自己的业务而定。个人建议可以先调整为 16 即可。
MySQL 随着连接数的增加性能是会下降的,可以让开发配合设置 thread pool,连接复用。在MySQL商业版中加入了thread pool这项功能
另外对于有的监控程序会读取 information_schema 下面的表,可以考虑关闭下面的参数
innodb_stats_on_metadata=0
set global innodb_stats_on_metadata=0
Top 2:(主从复制报错类型)
Last_SQL_Errno: 1062 (从库与主库数据冲突)
Last_Errno: 1062
Last_Error: Could not execute Write_rows event on table test.t;
Duplicate entry '4' for key 'PRIMARY',
Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY;
the event's master log mysql-bin.000014, end_log_pos 1505
针对这个报错,我们首先要考虑是不是在从库中误操作导致的。结果发现,我们在从库中进行了一条针对有主键表的 sql 语句的插入,导致主库再插入相同 sql 的时候,主从状态出现异常。发生主键冲突的报错。
解决方法:
在确保主从数据一致性的前提下,可以在从库进行错误跳过。一般使用 percona-toolkit 中的 pt-slave-restart 进行。
在从库完成如下操作
[root@zs bin]# ./pt-slave-restart -uroot -proot123
2017-07-20T14:05:30 p=...,u=root node4-relay-bin.000002 1506 1062
之后最好在从库中开启 read_only 参数,禁止在从库进行写入操作
Last_IO_Errno: 1593(server-id冲突)
Last_IO_Error:
Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids;
these ids must be different for replication to work
(or the --replicate-same-server-id option must be used on slave but this
does not always make sense; please check the manual before using it)
这个报错出现之后,就看一目了然看到两台机器的 server-id 是一样的。
在搭建主从复制的过程中,我们要确保两台机器的 server-id 是唯一的。这里再强调一下 server-id 的命名规则(服务器 ip 地址的最后一位+本 MySQL 服务的端口号)
解决方法:
在主从两台机器上设置不同的 server-id。
Last_SQL_Errno: 1032(从库少数据,主库更新的时候,从库报错)
Last_SQL_Error:
Could not execute Update_rows event on table test.t; Can't find record
in 't', Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the
event's master log mysql-bin.000014, end_log_pos 1708
解决问题的办法:
根据报错信息,我们可以获取到报错日志和position号,然后就能找到主库执行的哪条sql,导致的主从报错。
在主库执行:
/usr/local/mysql/bin/mysqlbinlog --no-defaults -v -v --base64-output=decode-rows /data/mysql/mysql-bin.000014 |grep -A 10 1708 > 1.log
cat 1.log
#170720 14:20:15 server id 3 end_log_pos 1708 CRC32 0x97b6bdec Update_rows: table id 113 flags: STMT_END_F
### UPDATE `test`.`t`
### WHERE
### @1=4 /* INT meta=0 nullable=0 is_null=0 */
### @2='dd' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
### SET
### @1=4 /* INT meta=0 nullable=0 is_null=0 */
### @2='ddd' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
# at 1708
#170720 14:20:15 server id 3 end_log_pos 1739 CRC32 0xecaf1922 Xid = 654
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
获取到 sql 语句之后,就可以在从库反向执行 sql 语句。把从库缺少的 sql 语句补全,解决报错信息。
在从库依次执行:
mysql> insert into t (b) values ('ddd');
Query OK, 1 row affected (0.01 sec)
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[root@node4 bin]# ./pt-slave-restart -uroot -proot123
2017-07-20T14:31:37 p=...,u=root node4-relay-bin.000005 283 1032
Top 3:MySQL安装过程中的报错
[root@zs data]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &[1] 3758
[root@zs data]# 170720 14:41:24 mysqld_safe Logging to '/data/mysql/error.log'.
170720 14:41:24 mysqld_safe Starting mysqld daemon with databases from /data/mysql170720
14:41:25 mysqld_safe mysqld from pid file /data/mysql/node4.pid ended
170720 14:41:24 mysqld_safe Starting mysqld daemon with databases from /data/mysql2017-07-20
14:41:25 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated.
Please use --explicit_defaults_for_timestamp server option
(see documentation for more details)./usr/local/mysql/bin/mysqld:
File '/data/mysql/mysql-bin.index' not found (Errcode: 13 - Permission denied)
2017-07-20 14:41:25 4388 [ERROR] Aborting
解决思路:
遇到这样的报错信息,我们要学会时时去关注错误日志 error log 里面的内容。看见了关键的报错点 Permission denied。证明当前 MySQL 数据库的数据目录没有权限。
解决方法:
[root@zs data]# chown mysql:mysql -R mysql
[root@zs data]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &
[1] 4402
[root@zs data]# 170720 14:45:56 mysqld_safe Logging to '/data/mysql/error.log'.
170720 14:45:56 mysqld_safe Starting mysqld daemon with databases from /data/mysql
启动成功。
如何避免这类问题,个人建议在安装 MySQL 初始化的时候,一定加上--user=mysql,这样就可以避免权限问题。
./mysql_install_db --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --defaults-file=/etc/my.cnf --user=mysql
Top 4:数据库密码忘记的问题
[root@zs ~]# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@zs ~]# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
我们有可能刚刚接手别人的 MySQL 数据库,而且没有完善的交接文档。root 密码可以丢失或者忘记了。
解决思路:
目前是进入不了数据库的情况,所以我们要考虑是不是可以跳过权限。因为在数据库中,mysql数据库中user表记录着我们用户的信息。
解决方法:
启动 MySQL 数据库的过程中,可以这样执行:
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables &
这样启动,就可以不用输入密码,直接进入 mysql 数据库了。然后在修改你自己想要改的root密码即可。
update mysql.user set password=password('root123') where user='root';
Top 5:truncate 删除数据,导致自动清空自增ID,前端返回报错 not found。
这个问题的出现,就要考虑下 truncate 和 delete 的区别了。
看下实验演练:
首先先创建一张表;
CREATE TABLE `t` (
`a` int(11) NOT NULL AUTO_INCREMENT,
`b` varchar(20) DEFAULT NULL,
PRIMARY KEY (`a`),
KEY `b` (`b`)
) ENGINE=InnoDB AUTO_INCREMENT=300 DEFAULT CHARSET=utf8
插入三条数据:
mysql> insert into t (b) values ('aa');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t (b) values ('bb');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t (b) values ('cc');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t;
+-----+------+
| a | b |
+-----+------+
| 300 | aa |
| 301 | bb |
| 302 | cc |
+-----+------+
3 rows in set (0.00 sec)
先用 delete 进行删除全表信息,再插入新值。
结果发现 truncate 把自增初始值重置了,自增属性从1开始记录了。当前端用主键id进行查询时,就会报没有这条数据的错误。
个人建议不要使用 truncate 对表进行删除操作,虽然可以回收表空间,但是会涉及自增属性问题。这些坑,我们不要轻易钻进去。
Top 6:
阿里云 MySQL 的配置文件中,需要注意一个参数设置就是:
lower_case_table_names = 0;默认情况
lower_case_table_names = 1;是不区分大小写 . 如果报你小写的表名找不到, 那你就把远端数据库的表名改成小写 , 反之亦然 . 注意 Mybatis 的 Mapper 文件的所有表名也要相应修改
Top 7:
有同学经常会问张老师,为什么我的数据库总会出现中文乱码的情况。一堆????不知道怎么回事。当向数据库中写入创建表,并插入中文时,会出现这种问题。此报错会涉及数据库字符集的问题。
解决思路:
对于中文乱码的情况,记住老师告诉你的三个统一就可以。还要知道在目前的mysql数据库中字符集编码都是默认的UTF8
处理办法:
1、数据终端,也就是我们连接数据库的工具设置为 utf8
2、操作系统层面;可以通过 cat /etc/sysconfig/i18n 查看;也要设置为 utf8
3、数据库层面;在参数文件中的 mysqld 下,加入 character-set-server=utf8。
Emoji 表情符号录入 mysql 数据库中报错。
Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x97\xF0\x9F...' for column 'CONTENT' at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2734)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1379)
解决思路:针对表情插入的问题,一定还是字符集的问题。
处理方法:我们可以直接在参数文件中,加入
vim /etc/my.cnf
[mysqld]
init-connect='SET NAMES utf8mb4'
character-set-server=utf8mb4
注:utf8mb4 是 utf8 的超集。
Top 8:使用 binlog_format=statement 这种格式,跨库操作,导致从库丢失数据,用户访问导致出现错误数据信息。
当前数据库二进制日志的格式为:binlog_format=statement
在主库设置binlog-do-db=mydb1(只同步mydb1这一个库)
在主库执行use mydb2;
insert into mydb1.t1 values ('bb');这条语句不会同步到从库。
但是这样操作就可以;
use mydb1;
insert into mydb1.t1 values ('bb');因为这是在同一个库中完成的操作。
在生产环境中建议使用binlog的格式为row,而且慎用binlog-do-db参数。
Top 9:MySQL 数据库连接超时的报错 ;
org.hibernate.util.JDBCExceptionReporter - SQL Error:0, SQLState: 08S01
org.hibernate.util.JDBCExceptionReporter - The last packet successfully received from the server was43200 milliseconds ago.The last packet sent successfully to the server was 43200 milliseconds ago, which is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection 'autoReconnect=true' to avoid this problem.
org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.exception.JDBCConnectionException: Could not execute JDBC batch update
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Connection.close() has already been called. Invalid operation in this state.
org.hibernate.util.JDBCExceptionReporter - SQL Error:0, SQLState: 08003
org.hibernate.util.JDBCExceptionReporter - No operations allowed after connection closed. Connection was implicitly closed e to underlying exception/error:
** BEGIN NESTED EXCEPTION **
大多数做 DBA 的同学,可能都会被开发人员告知,你们的数据库报了这个错误了。赶紧看看是哪里的问题。
这个问题是由两个参数影响的,wait_timeout 和 interactive_timeout。数据默认的配置时间是28800(8小时)意味着,超过这个时间之后,MySQL 数据库为了节省资源,就会在数据库端断开这个连接,Mysql服务器端将其断开了,但是我们的程序再次使用这个连接时没有做任何判断,所以就挂了。
解决思路:
先要了解这两个参数的特性;这两个参数必须同时设置,而且必须要保证值一致才可以。
我们可以适当加大这个值,8小时太长了,不适用于生产环境。因为一个连接长时间不工作,还占用我们的连接数,会消耗我们的系统资源。
解决方法:
可以适当在程序中做判断;强烈建议在操作结束时更改应用程序逻辑以正确关闭连接;然后设置一个比较合理的timeout的值(根据业务情况来判断)
Top 10 :can't open file (errno:24)
有的时候,数据库跑得好好的,突然报不能打开数据库文件的错误了。
解决思路:
首先我们要先查看数据库的 error log。然后判断是表损坏,还是权限问题。还有可能磁盘空间不足导致的不能正常访问表;操作系统的*也要关注下;用 perror 工具查看具体错误!
linux:/usr/local/mysql/bin # ./perror 24
OS error code 24: Too many open files
超出最大打开文件数*!ulimit -n查看系统的最大打开文件数是65535,不可能超出!那必然是数据库的最大打开文件数超出*!
在 MySQL 里查看最大打开文件数*命令:show variables like 'open_files_limit';
发现该数值过小,改为2048,重启 MySQL,应用正常
处理方法:
repair table ;
chown mysql权限
清理磁盘中的垃圾数据
mybatis支持postgresql吗
<mapper namespace="com.jim.logstashmvc.dao.generated.mapper.ProductMapper"> <resultMap id="BaseResultMap" type="com.jim.logstashmvc.dao.generated.entity.Product"> <!-- WARNING - @mbggenerated --> <id column="id" jdbcType="BIGINT" property="id" /> <result column="name" jdbcType="VARCHAR" property="name" /> </resultMap></mapper>
public interface ProductMapper extends Mapper<Product> {}
如何与spring集成?
<!-- MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- Spring集成 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <!-- MBG --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>${MBG.version}</version> <scope>compile</scope> <optional>true</optional> </dependency> <!-- 分页 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper.version}</version> </dependency> <!-- 通用Mapper --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>${mapper.version}</version> </dependency> <!-- TkMybatis 会使用到JPA的注解 --> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.3-1102-jdbc41</version> </dependency>
<!--MBG--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>${MBG.version}</version> <configuration> <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> <dependencies> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.3-1102-jdbc41</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>${mapper.version}</version> </dependency> </dependencies> </plugin>
注意下targetRuntime,这里采用的是MyBatis3Simple,它的默认选项是MyBatis3。如果采用通用mapper,我们在spring扫描接口时可以这样写。
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="jimSqlSessionFactory"/> <property name="basePackage" value="com.jim.logstashmvc.dao.generated.mapper"/> </bean>
如果是MyBatis3,生成的mapper.xml格式会复杂很多,我之前遇到过这样的问题:使用MyBatis3生成的mapper.xml然后错误 的配置了MapperScannerConfigurer为下面通用mapper模式,提示我的错误如下,原因可以认定是配置问题(不是某个mapper.xml中的id重复问题) ,后续再研究下非通用mapper的配置。
Caused by: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.jim.logstashmvc.dao.generated.mapper.ProductMapper.selectByExampleat org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:837)at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:809)
上面的报错信息已经找到,原因是因为采用了MyBatis3方式生成Mapper,但同时在配置文件中错误的增加了通用Mapper的插件,它会在Mapper接口上增加一个Mapper<T>的继承,而一旦继承了这个通过接口,它里面包含的方法Id与MyBatis3生成的方法Id正好重复,导致了在编译时提示上面的错误。
使用了通用Mapper之后,有个缺点就是使用Example时没有强类型的方法了,比如不能这样:
ProductExample example =new ProductExample(); ProductExample.Criteria criteria=example.createCriteria(); if (product.getId() != null) { criteria.andIdEqualTo(product.getId()); }
而只能这样写,字段名称需要以字符串的形式进行指定。这种方式非常适合于动态构建查询,比如:列表页的动态条件搜索
Example example = new Example(Product.class); Example.Criteria criteria = example.createCriteria(); if (product.getId() != null) { criteria.andEqualTo("id", product.getId()); }
我们之前的项目为了能够使用动态条件构建,又想使用强类型的Example进行简单的查询,我们扩展了targetRuntime,让其生成的Mapper即继承自Map<T>又生成了具体的方法,只不过生成的具体方法在名称上做了调整,在名称中间增加了一个独有的占有符,默认MBG生成的查询方法名称是selectByExample,这里我们另外生成一个selectByConcreteExample。这样做也是有代价的,生成的mapper.xml也不再只包含实体映射,mapper接口也不再只是继承的近似空接口了,具体怎么用仁者见仁吧。
public interface ImageMapper extends PartyInterface, RowBoundsMapper<Image>, BaseMapper<Image>, ExampleMapper<Image> { int countByConcreteExample(ImageExample example); int deleteByConcreteExample(ImageExample example); List<Image> selectByConcreteExample(ImageExample example); int updateByConcreteExampleSelective(@Param("record") Image record, @Param("example") ImageExample example); int updateByConcreteExample(@Param("record") Image record, @Param("example") ImageExample example);}
生成器的配置详细如下:
<generatorConfiguration> <properties resource="config.properties"/> <context id="jim" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <plugin type="${mapper.plugin}"> <property name="mappers" value="${mapper.Mapper}"/> </plugin> <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"> </jdbcConnection> <javaModelGenerator targetPackage="${targetModelPackage}" targetProject="${targetJavaProject}"/> <sqlMapGenerator targetPackage="mapper" targetProject="${targetResourcesProject}"/> <javaClientGenerator targetPackage="${targetMapperPackage}" targetProject="${targetJavaProject}" type="XMLMAPPER"> </javaClientGenerator> <table tableName="product" domainObjectName="Product"></table> </context></generatorConfiguration>
<bean id="jimDataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClass}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="initialSize" value="5"/> <property name="minIdle" value="10"/> <property name="maxWait" value="60000"/> <property name="timeBetweenEvictionRunsMillis" value="60000"/> <property name="minEvictableIdleTimeMillis" value="3600000"/> <property name="validationQuery" value="SELECT 1"/> <property name="testWhileIdle" value="true"/> <property name="testOnBorrow" value="false"/> <property name="testOnReturn" value="false"/> </bean> <bean id="jimSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="jimDataSource"/> <property name="mapperLocations" value="classpath:mapper/*.xml"/> <property name="typeAliasesPackage" value="com.jim.logstashmvc.dao.generated.entity"/> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageHelper"> <property name="properties"> <value> dialect=postgresql reasonable=true supportMethodsArguments=true returnPageInfo=check params=count=countSql </value> </property> </bean> </array> </property> </bean> <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="jimSqlSessionFactory"/> <property name="basePackage" value="com.jim.logstashmvc.dao.generated.mapper"/> </bean>
@Servicepublic interface IService<T> { T selectByKey(Object key); int save(T entity); int delete(Object key); int updateAll(T entity); int updateNotNull(T entity); List<T> selectByExample(Object example); //TODO 其他...}
public abstract class BaseService<T> implements IService<T> { @Autowired protected Mapper<T> mapper; public Mapper<T> getMapper() { return mapper; } @Override public T selectByKey(Object key) { return mapper.selectByPrimaryKey(key); } public int save(T entity) { return mapper.insert(entity); } public int delete(Object key) { return mapper.deleteByPrimaryKey(key); } public int updateAll(T entity) { return mapper.updateByPrimaryKey(entity); } public int updateNotNull(T entity) { return mapper.updateByPrimaryKeySelective(entity); } public List<T> selectByExample(Object example) { return mapper.selectByExample(example); } //TODO 其他...}
@Servicepublic class ProductServiceImpl extends BaseService<Product> implements ProductService { @Override public List<Product> selectByProduct(Product product, int page, int rows) { Example example = new Example(Product.class); Example.Criteria criteria = example.createCriteria(); if(!StringUtils.isBlank(product.getName())){ criteria.andEqualTo("name",product.getName()); } if (product.getId() != null) { criteria.andEqualTo("id", product.getId()); } PageHelper.startPage(page, rows); return selectByExample(example); }}
安装postgresql并且成功远程连接,集成MBG生成mapper以及model,然后将mybatis与spring集成,最后通过通用mapper中联起来就达到了我们的目的:通过少量的代码完成大部分的工作,重复劳动交给工具完成。但通用mapper有它的优点也就有它的缺点,需要根据项目环境来平衡,个人感觉利大于弊。
本文引用:
1:http://www.mybatis.tk/
2:https://github.com/abel533/Mybatis-Spring
mybatis+postgresql平台
标签:
mybatis支持postgresql吗
<mapper namespace="com.jim.logstashmvc.dao.generated.mapper.ProductMapper"> <resultMap id="BaseResultMap" type="com.jim.logstashmvc.dao.generated.entity.Product"> <!-- WARNING - @mbggenerated --> <id column="id" jdbcType="BIGINT" property="id" /> <result column="name" jdbcType="VARCHAR" property="name" /> </resultMap></mapper>
public interface ProductMapper extends Mapper<Product> {}
如何与spring集成?
<!-- MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- Spring集成 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <!-- MBG --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>${MBG.version}</version> <scope>compile</scope> <optional>true</optional> </dependency> <!-- 分页 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper.version}</version> </dependency> <!-- 通用Mapper --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>${mapper.version}</version> </dependency> <!-- TkMybatis 会使用到JPA的注解 --> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.3-1102-jdbc41</version> </dependency>
<!--MBG--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>${MBG.version}</version> <configuration> <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> <dependencies> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.3-1102-jdbc41</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>${mapper.version}</version> </dependency> </dependencies> </plugin>
注意下targetRuntime,这里采用的是MyBatis3Simple,它的默认选项是MyBatis3。如果采用通用mapper,我们在spring扫描接口时可以这样写。
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="jimSqlSessionFactory"/> <property name="basePackage" value="com.jim.logstashmvc.dao.generated.mapper"/> </bean>
如果是MyBatis3,生成的mapper.xml格式会复杂很多,我之前遇到过这样的问题:使用MyBatis3生成的mapper.xml然后错误 的配置了MapperScannerConfigurer为下面通用mapper模式,提示我的错误如下,原因可以认定是配置问题(不是某个mapper.xml中的id重复问题) ,后续再研究下非通用mapper的配置。
Caused by: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.jim.logstashmvc.dao.generated.mapper.ProductMapper.selectByExampleat org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:837)at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:809)
上面的报错信息已经找到,原因是因为采用了MyBatis3方式生成Mapper,但同时在配置文件中错误的增加了通用Mapper的插件,它会在Mapper接口上增加一个Mapper<T>的继承,而一旦继承了这个通过接口,它里面包含的方法Id与MyBatis3生成的方法Id正好重复,导致了在编译时提示上面的错误。
使用了通用Mapper之后,有个缺点就是使用Example时没有强类型的方法了,比如不能这样:
ProductExample example =new ProductExample(); ProductExample.Criteria criteria=example.createCriteria(); if (product.getId() != null) { criteria.andIdEqualTo(product.getId()); }
而只能这样写,字段名称需要以字符串的形式进行指定。这种方式非常适合于动态构建查询,比如:列表页的动态条件搜索
Example example = new Example(Product.class); Example.Criteria criteria = example.createCriteria(); if (product.getId() != null) { criteria.andEqualTo("id", product.getId()); }
我们之前的项目为了能够使用动态条件构建,又想使用强类型的Example进行简单的查询,我们扩展了targetRuntime,让其生成的Mapper即继承自Map<T>又生成了具体的方法,只不过生成的具体方法在名称上做了调整,在名称中间增加了一个独有的占有符,默认MBG生成的查询方法名称是selectByExample,这里我们另外生成一个selectByConcreteExample。这样做也是有代价的,生成的mapper.xml也不再只包含实体映射,mapper接口也不再只是继承的近似空接口了,具体怎么用仁者见仁吧。
public interface ImageMapper extends PartyInterface, RowBoundsMapper<Image>, BaseMapper<Image>, ExampleMapper<Image> { int countByConcreteExample(ImageExample example); int deleteByConcreteExample(ImageExample example); List<Image> selectByConcreteExample(ImageExample example); int updateByConcreteExampleSelective(@Param("record") Image record, @Param("example") ImageExample example); int updateByConcreteExample(@Param("record") Image record, @Param("example") ImageExample example);}
生成器的配置详细如下:
<generatorConfiguration> <properties resource="config.properties"/> <context id="jim" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <plugin type="${mapper.plugin}"> <property name="mappers" value="${mapper.Mapper}"/> </plugin> <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"> </jdbcConnection> <javaModelGenerator targetPackage="${targetModelPackage}" targetProject="${targetJavaProject}"/> <sqlMapGenerator targetPackage="mapper" targetProject="${targetResourcesProject}"/> <javaClientGenerator targetPackage="${targetMapperPackage}" targetProject="${targetJavaProject}" type="XMLMAPPER"> </javaClientGenerator> <table tableName="product" domainObjectName="Product"></table> </context></generatorConfiguration>
<bean id="jimDataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClass}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="initialSize" value="5"/> <property name="minIdle" value="10"/> <property name="maxWait" value="60000"/> <property name="timeBetweenEvictionRunsMillis" value="60000"/> <property name="minEvictableIdleTimeMillis" value="3600000"/> <property name="validationQuery" value="SELECT 1"/> <property name="testWhileIdle" value="true"/> <property name="testOnBorrow" value="false"/> <property name="testOnReturn" value="false"/> </bean> <bean id="jimSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="jimDataSource"/> <property name="mapperLocations" value="classpath:mapper/*.xml"/> <property name="typeAliasesPackage" value="com.jim.logstashmvc.dao.generated.entity"/> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageHelper"> <property name="properties"> <value> dialect=postgresql reasonable=true supportMethodsArguments=true returnPageInfo=check params=count=countSql </value> </property> </bean> </array> </property> </bean> <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="jimSqlSessionFactory"/> <property name="basePackage" value="com.jim.logstashmvc.dao.generated.mapper"/> </bean>
@Servicepublic interface IService<T> { T selectByKey(Object key); int save(T entity); int delete(Object key); int updateAll(T entity); int updateNotNull(T entity); List<T> selectByExample(Object example); //TODO 其他...}
public abstract class BaseService<T> implements IService<T> { @Autowired protected Mapper<T> mapper; public Mapper<T> getMapper() { return mapper; } @Override public T selectByKey(Object key) { return mapper.selectByPrimaryKey(key); } public int save(T entity) { return mapper.insert(entity); } public int delete(Object key) { return mapper.deleteByPrimaryKey(key); } public int updateAll(T entity) { return mapper.updateByPrimaryKey(entity); } public int updateNotNull(T entity) { return mapper.updateByPrimaryKeySelective(entity); } public List<T> selectByExample(Object example) { return mapper.selectByExample(example); } //TODO 其他...}
@Servicepublic class ProductServiceImpl extends BaseService<Product> implements ProductService { @Override public List<Product> selectByProduct(Product product, int page, int rows) { Example example = new Example(Product.class); Example.Criteria criteria = example.createCriteria(); if(!StringUtils.isBlank(product.getName())){ criteria.andEqualTo("name",product.getName()); } if (product.getId() != null) { criteria.andEqualTo("id", product.getId()); } PageHelper.startPage(page, rows); return selectByExample(example); }}
安装postgresql并且成功远程连接,集成MBG生成mapper以及model,然后将mybatis与spring集成,最后通过通用mapper中联起来就达到了我们的目的:通过少量的代码完成大部分的工作,重复劳动交给工具完成。但通用mapper有它的优点也就有它的缺点,需要根据项目环境来平衡,个人感觉利大于弊。
本文引用:
1:http://www.mybatis.tk/
2:https://github.com/abel533/Mybatis-Spring
mybatis+postgresql平台
标签:
mybatis 配置postgresql 和mysql的区别
MySQL的主要优点 (速度,流行,window,线程,事务) 1、 首先是速度,MySQL通常要比PostgreSQL快得多。MySQL自已也宣称速度是他们追求的主要目标之一,基于这个原因,MySQL在以前的文档中也曾经说过并不准备支持事务和触发器。
mybatis 配置postgresql 和mysql的区别
MySQL的主要优点 (速度,流行,window,线程,事务) 1、 首先是速度,MySQL通常要比PostgreSQL快得多。MySQL自已也宣称速度是他们追求的主要目标之一,基于这个原因,MySQL在以前的文档中也曾经说过并不准备支持事务和触发器。
如何打好Java的基础?
从大学到现在,我使用Java已经将近20年,日常也带实习生,还在公司内部做training,所以可以分享下我的经验,希望对你有用。
因为是在工作中培训,就必然有两个约束:实用、时间紧,因此就不能像大学那样,把所有的知识点都面面俱到的讲到。而只能挑基础的,实用的,难理解的讲。至于其他边边角角的知识,就一笔带过。一则没有时间,二则不常用,再则既使讲了,学生印象也不深刻。总之一句话:“好钢用在刀刃上”。
下面,就根据我的实践,具体谈下学习过程:
1.基础知识
我学习java的时候,先是通读了《Java编程思想》,然后是《Java核心技术》。当时这两本书还不像现在这么厚,而刚才我把案头的《Java核心技术》第9版翻了翻,上下两册已经1700多页了,可想而知,如果要把它通读一遍,且不说把所有的代码都调通,就是当小说读,估计也需要些时间。
但我现在教学依然首推《Java核心技术》,主要是体系完整,实例多,可操作性强。但对初学者,我一般是只讲前6章,也就是下面的内容:
Java程序设计概述
Java程序设计环境
Java的基础程序设计结构
对象与类
继承
接口与内部类
就《Java核心技术》第9版来说,也就是到250页为止,加把劲,1个月拿下完全没问题。
因为你是自学,所以建议你一定要把其中的代码都调通,课后的作业尽量去做。除此之外,还有两点特别重要:
#.学习笔记
因为你是自学,不像在企业中学了就能够实践,印象自然特别深刻。而自学因为没有实践的及时反馈,所以记笔记就显得特别重要。因为记笔记就像写作一样,是整理思路的绝佳方法。同时学习笔记也是你以后开发,面试的绝好资料。
学习编程,人跟人是不一样的,别人觉得难理解的东西,对你却不一定;而你觉得难理解的东西,别人可能又会觉得特简单。而学习笔记就是自己专有的“难点手册”,有点像高考时的“错题本”,以后无论是在面试前,还是在日常工作中,随时都可以翻出来看看,自是获益匪浅。
#.分门别类保存demo
学习笔记是很好的文字资料,但编程界有句话说的特别好,所谓“no code, no text”,意思就是说:千言万语都没有一段代码来的实在。
以我的经验,在你在学习的过程中,就某个知识点,无论当时理解的多透彻,调试的多棒,只要时间一长,等到了实用的时候,肯定会碰到各种各样的问题,一些看似简单的东西,此时死活就是调不通,正所谓人到事中迷。这个时候,如果你手头恰有运行良好的demo,打开参考一下(甚至直接拷贝过来),问题自然迎刃而解。而且因为这些demo都是你亲手调试出来,印象自然特别深刻,一碰到问题,在脑子中自会立刻涌现。
所以说,在学习的过程,一定要善待你调通的demo,千万不要用完了就扔,等后来碰到困难,想要用时却找不到,追愧莫及。正确的做法就是把所有调通的demo,分门别类的保存起来,到时候查起来自是得心应手。
人都说“书到用时方恨少”,其实代码也是这样,所谓“demo用时方恨少”。
2.Spring
目前在Java EE开发中,Spring已经成为和Java核心库一样的基础设施,所以说如果想成为一个合格的Java程序员,Spring肯定绕不开。另一方面,如果掌握了Spring体系,Java基本上就算入门了,就有能力进行一些实用级的开发了。
但Spring本身也是日渐复杂,衍生项目越来越多,但最最核心的概念依旧是IOC和AOP,掌握了这两个概念,再把Spring MVC学会,再学习其他的衍生项目就会平滑很多。
同时,因为Spring本身就应用了许多优雅的设计理念,所以学习Spring的过程,也是加强Java基础知识学习的过程。因此等你掌握了Spring,原来很多你理解不透彻的Java特性,此时就会恍然大悟,包括接口、抽象类等。
我学习Spring,读的第一本书是《Spring实战》,坦率的说,书很一般,但市面上比它好的书,我却没有遇到过。还有一本《Spring源码深度解析》也不错,对Spring的设计理念讲的尤其透彻,虽然整本书读起来有些艰涩,但前几章却生动有趣,也是整本书的精华。所以建议你在学习Spring之前,先把该书的前几章通读一下,然后再回过头来学习《Spring实战》会顺利很多。
以我经验,要学透Spring,终极的方法还是阅读源码(我当时就是这么干的),待把Spring的核心源码通读了,人就真的自由了(所谓无不自由),不仅是对Spring,而是对整个Java体系。以后再遇到其他框架,大概一眼就能看出其中的脉络,所谓到了“看山不是山”的境界。但这都是后话,可以作为以后你努力的方向。
和学习Java基础知识一样,学习Spring也一定要记笔记,一定要分门别类保存demo。
老实说,Spring对初学者不算简单,因此最好能有个好老师带一下,不用太长时间,2个课时即可,然后就是在你遇到大的困难时,能及时的点拨下。
以我的经验,要初步掌握Spring,大概需要1到1个半月的时间。
3.其他知识
Spring是Java编程的基础设施,但真要进入到实际项目的开发,还有些东西绕不过,包括 MySql,Mybatis,Redis,Servlet等,但如果你经过Spring的洗礼,这些东西相对就简单多了,以我的经验,1个月的时间足够了。
4.实践
学习Java,光学不练肯定是不行的。但因为是自学,所以就没有实际的产品让你练手,但也没有关系,谁大学还没有做过毕业设计呢?以我的经验,大家最爱的“学生管理系统”依旧是个很好的练手系统。
别看“学生管理系统”逻辑简单,但麻雀虽小五脏俱全,其中数据库设计、Mybatis,Spring、SpringMVC,Servlet、Tomcat一个都不缺,绝对的练手好伴侣。
还有,虽然你的学习重点在Java,因为要做一个完整的demo,前端的配合肯定少不了。因此就免少不了要学一些简单的JS、HTML知识,但因为前端本就是个很大的topic,所以一定要控制好边界,千万不要顾此失彼。就“学生管理系统”来说,在前端上,只要实现一个包含table、textbox、button,能发送REST请求到server,能实现学生的“增删改查”的简单页面即可。
作为一个练手项目,目标就是把Java的主要技能点串起来,所以自不求尽善尽美(也不可能),所以1个月时间足够了。
.最后
按照上面的过程,4个月的时间刚刚好。当然Java的体系是很庞大的,还有很多更高级的技能需要掌握,但不要着急,这些完全可以放到以后工作中边用别学。
学习编程就是一个由混沌到有序的过程,所以你在学习过程中,如果一时碰到理解不了的知识点,大可不必沮丧,更不要气馁,这都是正常的不能再正常的事情了,不过是“人同此心,心同此理”的暂时而已。
在日常的教学中,我常把下面这句话送给学员们,今天也把它送给你:
“道路是曲折的,前途是光明的!”
祝你好运!
[20分] mybatis怎样配置支持并发
参考配置方法如下:
1、修改SqlSessionFactoryBean,或者继承该类, 增加configLocations, 移除 configLocation
private Resource[] configLocations;
/*修改该方法*/
public void setConfigLocation(Resource configLocation){
this.configLocations = configLocation != null ? new Resource[] { configLocation } : null;
}
/*增加该方法*/
public void setConfigLocations(Resource[] configLocations) {
this.configLocations = configLocations;
}
/**
* 合并mybatis配置文件
*/
public Document SQLConfigMap()
{
Document doc = DocumentHelper.createDocument();
doc.setXMLEncoding("UTF-8");
DocumentFactory documentFactory = new DocumentFactory();
DocumentType docType = documentFactory.createDocType("configuration",
"-//mybatis.org//DTD Config 3.0//EN", "http://mybatis.org/dtd/mybatis-3-config.dtd");
doc.setDocType(docType);
Element rootElement = doc.addElement("configuration");
rootElement.addElement("typeAliases");
rootElement.addElement("mappers");
return doc;
}
public void readXML(Resource configXML, final Element elementTypeAlias,
final Element elementMapper)
{
// Document document = null;
SAXReader saxReader = new SAXReader();
// Element root = doc.getRootElement();
/*typeAliases合并*/
saxReader.addHandler("/configuration/typeAliases/typeAlias", new ElementHandler()
{
@Override
public void onEnd(ElementPath path)
{
Element row = path.getCurrent();
Element els = elementTypeAlias.addElement("typeAlias");
els.addAttribute("alias", row.attributeValue("alias")).addAttribute("type",
row.attributeValue("type"));
row.detach();
}
@Override
public void onStart(ElementPath arg0)
{
// TODO Auto-generated method stub
}
});
/*mapper合并*/
saxReader.addHandler("/configuration/mappers/mapper", new ElementHandler()
{
@Override
public void onEnd(ElementPath path)
{
Element row = path.getCurrent();
Element els = elementMapper.addElement("mapper");
String mapper = row.attributeValue("mapper");
String resource = row.attributeValue("resource");
els.addAttribute("mapper", mapper);
els.addAttribute("resource", resource);
row.detach();
}
@Override
public void onStart(ElementPath arg0)
{
// TODO Auto-generated method stub
}
});
try
{
saxReader.read(configXML.getInputStream());
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//return doc;
}
/**
* Build a {@code SqlSessionFactory} instance.
*
* The default implementation uses the standard MyBatis {@code XMLConfigBuilder}
* API to build a
* {@code SqlSessionFactory} instance based on an Reader.
*
* @return SqlSessionFactory
* @throws IOException if loading the config file failed
*/
protected SqlSessionFactory buildSqlSessionFactory() throws IOException
{
Configuration configuration = null;
XMLConfigBuilder xmlConfigBuilder = null;
Document document = this.SQLConfigMap();
Element root = document.getRootElement();
Element elementMapper = root.element("mappers");
Element elementTypeAlias = root.element("typeAliases");
for (Resource configLocation : configLocations)
{
readXML(configLocation, elementTypeAlias, elementMapper);
}
// Reader reader = null; InputStream inputStream = null;
if (this.configLocations != null)
{
logger.debug(document.asXML());
InputStream inputSteam = new ByteArrayInputStream(document.asXML().getBytes());
xmlConfigBuilder = new XMLConfigBuilder(inputSteam, null, this.configurationProperties);
configuration = xmlConfigBuilder.getConfiguration();
if (inputSteam != null)
{
inputSteam.close();
inputSteam = null;
}
document = null;
}
else
{
if (this.logger.isDebugEnabled())
{
this.logger.debug("Property 'configLocation' not specified,
using default MyBatis Configuration");
}
configuration = new Configuration();
configuration.setVariables(this.configurationProperties);
}
if (hasLength(this.typeAliasesPackage))
{
String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage,
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
for (String packageToScan : typeAliasPackageArray)
{
configuration.getTypeAliasRegistry().registerAliases(packageToScan);
if (this.logger.isDebugEnabled())
{
this.logger.debug("Scanned package: '" + packageToScan + "' for aliases");
}
}
}
if (!isEmpty(this.typeAliases))
{
for (Class<?> typeAlias : this.typeAliases)
{
configuration.getTypeAliasRegistry().registerAlias(typeAlias);
if (this.logger.isDebugEnabled())
{
this.logger.debug("Registered type alias: '" + typeAlias + "'");
}
}
}
if (!isEmpty(this.plugins))
{
for (Interceptor plugin : this.plugins)
{
configuration.addInterceptor(plugin);
if (this.logger.isDebugEnabled())
{
this.logger.debug("Registered plugin: '" + plugin + "'");
}
}
}
if (hasLength(this.typeHandlersPackage))
{
String[] typeHandlersPackageArray = tokenizeToStringArray(this.typeHandlersPackage,
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
for (String packageToScan : typeHandlersPackageArray)
{
configuration.getTypeHandlerRegistry().register(packageToScan);
if (this.logger.isDebugEnabled())
{
this.logger.debug("Scanned package: '" + packageToScan + "' for type handlers");
}
}
}
if (!isEmpty(this.typeHandlers))
{
for (TypeHandler<?> typeHandler : this.typeHandlers)
{
configuration.getTypeHandlerRegistry().register(typeHandler);
if (this.logger.isDebugEnabled())
{
this.logger.debug("Registered type handler: '" + typeHandler + "'");
}
}
}
if (xmlConfigBuilder != null)
{
try
{
xmlConfigBuilder.parse();
if (this.logger.isDebugEnabled())
{
this.logger.debug("Parsed configuration file: '" + this.configLocations + "'");
}
}
catch (Exception ex)
{
throw new NestedIOException("Failed to parse config resource: " +
this.configLocations, ex);
}
finally
{
ErrorContext.instance().reset();
}
}
if (this.transactionFactory == null)
{
this.transactionFactory = new SpringManagedTransactionFactory();
}
Environment environment = new Environment(this.environment, this.transactionFactory,
this.dataSource);
configuration.setEnvironment(environment);
if (this.databaseIdProvider != null)
{
try
{
configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));
}
catch (SQLException e)
{
throw new NestedIOException("Failed getting a databaseId", e);
}
}
if (!isEmpty(this.mapperLocations))
{
for (Resource mapperLocation : this.mapperLocations)
{
if (mapperLocation == null)
{
continue;
}
try
{
XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),
configuration, mapperLocation.toString(), configuration.getSqlFragments());
xmlMapperBuilder.parse();
}
catch (Exception e)
{
throw new NestedIOException("Failed to parse mapping resource: '" +
mapperLocation + "'", e);
}
finally
{
ErrorContext.instance().reset();
}
if (this.logger.isDebugEnabled())
{
this.logger.debug("Parsed mapper file: '" + mapperLocation + "'");
}
}
}
else
{
if (this.logger.isDebugEnabled())
{
this.logger.debug("Property 'mapperLocations' was not specified or
no matching resources found");
}
}
return this.sqlSessionFactoryBuilder.build(configuration);
}