1 关于java.sql.SQLException: Access denied for user ‘root’@‘localhost’ (using password: YES)问题解决
一定是你密码账号问题,
排除方法
<?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>
<!-- 环境 -->
<properties resource="xiyou/mq/map/dbconfig.properties"/>
<environments default="development">
<!-- id -->
<environment id="development">
<!-- 事务管理 -->
<transactionManager type="JDBC">
</transactionManager>
<!-- <dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/ttms" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource> -->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="xiyou/mq/map/Mapper.xml"/>
</mappers>
</configuration>
如果用我注释掉的DataSource来使用的时候有结果,采用下边那个没有结果,就是因为你设置数据库配置文件时出错了,每行最后都不能有空格,切记。
username=root
password=123456
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ttms?useUnicode=true&characterEncoding=utf-8
2 Fri Apr 20 14:02:30 CST 2018 WARN: Establishing SSL connection without server’s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn’t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false’. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
这个不是报错,不用管的
3.资源路径问题Could not find resource
<properties resource="xiyou/mq/map/dbconfig.properties"/>
<mappers>
<mapper resource="xiyou/mq/map/Mapper.xml"/>
src目录下的包(com或者org)/
</mappers>
映射文件
<?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="xiyou.mq.mp.Mapper">
命名空间一般是你的Mapper文件所在包路径,一定要保证这里的命名空间和后边SelectOne()里边的id前边所加的命名空间一致
<select id="findById" parameterType="int" resultType="xiyou.mq.pojo.Employee">
返回类型也是这样的
select * from employee where emp_id=#{emp_id}
</select>
</mapper>
测试用的类
package xiyou.mq.test;
import java.io.IOException;
import java.io.Reader;
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 xiyou.mq.pojo.Employee;
public class Testmq {
public static void main(String[] args) {
// TODO Auto-generated method stub
String resource="xiyou/mq/map/mybatisconfig.xml";
//String resource="xiyou/mq/map/mybatis-config.xml";
Reader reader=null;
SqlSession session;
System.out.println(" Testmq21");
try {
//InputStream inputStream=Resources.getResoutceAsStream(resource);
reader=Resources.getResourceAsReader(resource);
System.out.println(" Testmq21");
SqlSessionFactory sqlMapper=new SqlSessionFactoryBuilder().build(reader);
//SqlSwssion sqlSwssionFactory=new SqlSessionFactoryBuilder().build(inputStream);
session =sqlMapper.openSession();
Employee te=session.selectOne("xiyou.mq.map.Mapper.findById",1);
System.out.println(te.getEmp_name());
session.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
总结一下就是出现java类的地方采用包名org. 包名1.包名2.类名.ID值
引用XML的时候可以采用org/…/…/*.xml的方式
这里是固定的写法。我试了绝对路径发现不对的,下边是对的
String resource="xiyou/mq/map/mybatisconfig.xml";
<properties resource="xiyou/mq/map/dbconfig.properties"/>
dbconfig.properties内容
username=root
password=123456
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ttms?useUnicode=true&characterEncoding=utf-8
这个可以是<mapper namespace="xiyou.mq.map.Mapper">
<mapper namespace="Employee">
<select id="findById" parameterType="int" resultType="xiyou.mq.pojo.Employee">
这里必须这样写
select * from employee where emp_id=#{emp_id}
</select>
</mapper>
关于Springboot整合mybatis用的注解的感想
如果你需求简单,可以使用注解版,像我脑子瓦特掉了,花了好久去构建注解版的sql语句,我真的脑残
package edu.xiyou.dao;
import edu.xiyou.entity.CourseSelection;
import edu.xiyou.entity.CourseSelectionDTO;
import org.apache.ibatis.annotations.*;
import java.util.List;
/**
* Created by mmm on 2019/5/7.
* TODO
* e-mail : 13032946420@163.com
* date : 2019/5/721:29
* desc :
* version: 1.0
*/
public interface CourseSelectionDao {
@Insert("insert into course_selection(user_id,course_id,process)" +
" values(#{userId},#{courseId},#{process})")
int insert(CourseSelection courseSelection);
/**
* 单表查询course_selection
* @param userId
* @return
*/
@Select(
"<script>" +
"select * from course_selection " +
"<if test=\"userId !=null \">" +
"where user_id = #{userId} </if>" +
"</script>"
)
@Results({
@Result(property = "courseSelectionId", column = "course_selection_id"),
@Result(property = "userId", column = "user_id"),
@Result(property = "courseId", column = "course_id"),
@Result(property = "process", column = "process"),
@Result(property = "grade",column= "grade"),
@Result(property = "courseName",column= "courseName")
})
List<CourseSelection> selectByCond(@Param(value="userId") int userId);
/**
* 多表查询
* @param userId
* @return
*/
@Select("<script>"+
"select * from course_selection join course on course.course_id = course_selection.course_id "+
"<if test = \"userId != 0\"> where user_id = #{userId} </if>"+
"</script>"
)
@Results({
@Result(property = "courseSelectionId", column = "course_selection_id"),
@Result(property = "userId", column = "user_id"),
@Result(property = "courseId", column = "course_id"),
@Result(property = "process", column = "process"),
@Result(property = "grade",column= "grade"),
@Result(property = "courseName",column= "course_name")
})
List<CourseSelection> selectByCond2(@Param(value="userId") int userId);
@Delete("DELETE FROM course_selection WHERE course_selection_id =#{courseSelectionId}")
int deleteById(int courseSelectionId);
@Select("select * from course_selection where user_id=#{userId} and course_id = #{courseId}")
CourseSelection selectByUserIdAndCourseId(@Param(value = "userId") int userId,@Param(value = "courseId") int courseId);
@Update("update course_selection SET " +
"examgrade = #{examgrade},examawer =#{examawer},process =#{process},grade = #{grade} where course_selection_id= #{courseSelectionId}")
int updateSelection(CourseSelection courseSelection);
@Select("select * from course_selection where course_id = #{courseId}")
List<CourseSelection> selectCourseSelection(int courseId);
@Select("select examgrade,course_selection_id, student_name,student_no,process,grade,course.course_name,course_selection.course_id " +
"from course,course_selection left JOIN student on course_selection.user_id=student.student_id "+
"where course_selection.course_id = course.course_id and course_selection.course_id =#{courseId}")
@Results({
@Result(property = "courseSelectionId",column = "course_selection_id"),
@Result(property = "studentName", column = "student_name"),
@Result(property = "studentNo", column = "student_no"),
@Result(property = "process", column = "process"),
@Result(property = "grade",column= "grade"),
@Result(property = "courseName",column= "course_name")
})
List<CourseSelectionDTO> courseSelectionSelect(int courseId);
@Select("select * from course_selection where course_selection_id = #{courseSelectionId}")
CourseSelection courseSelectionSelecttByPrimary(int courseSelectionId);
@Select("SELECT count(*) a from course_selection where grade >= 60;")
int findScore();
}