报错:Communications link failure(弄了好久没解决)
package com.wf.mybatis.entity;
public class Goods {
private Integer goodsId;
private String title;
private String subTitle;
private Float originalCost;
private Float currentPrice;
private Float discount;
private Integer isFreeDelivery;
private Integer categoryId;
public Goods() {
}
public Goods(Integer goodsId, String title, String subTitle, Float originalCost, Float currentPrice, Float discount, Integer isFreeDelivery, Integer categoryId) {
this.goodsId = goodsId;
this.title = title;
this.subTitle = subTitle;
this.originalCost = originalCost;
this.currentPrice = currentPrice;
this.discount = discount;
this.isFreeDelivery = isFreeDelivery;
this.categoryId = categoryId;
}
public Integer getGoodsId() {
return goodsId;
}
public void setGoodsId(Integer goodsId) {
this.goodsId = goodsId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSubTitle() {
return subTitle;
}
public void setSubTitle(String subTitle) {
this.subTitle = subTitle;
}
public Float getOriginalCost() {
return originalCost;
}
public void setOriginalCost(Float originalCost) {
this.originalCost = originalCost;
}
public Float getCurrentPrice() {
return currentPrice;
}
public void setCurrentPrice(Float currentPrice) {
this.currentPrice = currentPrice;
}
public Float getDiscount() {
return discount;
}
public void setDiscount(Float discount) {
this.discount = discount;
}
public Integer getIsFreeDelivery() {
return isFreeDelivery;
}
public void setIsFreeDelivery(Integer isFreeDelivery) {
this.isFreeDelivery = isFreeDelivery;
}
public Integer getCategoryId() {
return categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
}
=============================================================================================
package com.wf.mybatis.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.Reader;
/**
* Mybatis工具类,创建全局唯一的SqlSessionFactory对象
*/
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory=null;
static {
Reader reader=null;
try {
reader=Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory= new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
}
public static SqlSession getSession(){
return sqlSessionFactory.openSession();
}
public static void closeSession(SqlSession sqlSession){
if(sqlSession!=null){
sqlSession.close();
}
}
}
==============================================================================================
<?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>
<settings>
<!-- 驼峰命名转换:goods_id->goodsId -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!-- 默认指向的数据库 -->
<environments default="dev">
<!-- 环境配置:不同的环境不同的id-->
<environment id="dev">
<transactionManager type="JDBC"></transactionManager>
<!-- 采用连接池的方式管理数据库连接-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/babytun?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="wf980321"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mappers/goods.xml"/>
</mappers>
</configuration>
============================================================================================
<?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="goods">
<select id="selectAll" resultType="com.wf.mybatis.entity.Goods">
select * from t_goods
</select>
</mapper>
==============================================================================================
package com.wf.mybatis;
import com.wf.mybatis.entity.Goods;
import com.wf.mybatis.utils.MybatisUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
public class MybatisTestor {
@Test
public void mybatisUtilsTest() throws Exception {
SqlSession sqlSession= null;
try {
sqlSession=MybatisUtils.getSession();
System.out.println(sqlSession);
} catch (Exception e) {
throw e;
}finally {
MybatisUtils.closeSession(sqlSession);
}
}
@Test
public void selectAllTest() throws Exception {
SqlSession sqlSession= null;
try {
sqlSession=MybatisUtils.getSession();
List<Goods> list = sqlSession.selectList("goods.selectAll");
for(Goods g:list){
System.out.println(g.getTitle());
}
} catch (Exception e) {
throw e;
}finally {
MybatisUtils.closeSession(sqlSession);
}
}
@Test
public void test01() throws IOException {
Reader reader= Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(reader);
SqlSession session=null;
session=sqlSessionFactory.openSession();
System.out.println(session);
if(session!=null){
session.close();
}
}
}
================================================================================================
<?xml version="1.0" encoding="UTF-8"?>
<project 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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wf</groupId>
<artifactId>mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
</dependencies>
</project>26
收起
正在回答 回答被采纳积分+1
1回答
java工程师2020版
- 参与学习 人
- 提交作业 9410 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星