idea版mybatis的⼊门及环境配置
idea版mybatis的⼊门及环境配置scroll lock怎么关
#简单来说,MySQL是jdbc的升级,将更多的注意⼒放在编写MySQL语句上,
这是我在b站上学习mybatis框架时遇到的问题,说实话卡了挺长时间了,基础不好,查了很多资料,试了很多⽅法才得以解决,鄙⼈确实⽐较菜
遇到的问题:
这只是其中⼀个问题,其他的没记录,就这⼀个保存下来了
Exception in thread “main” org.ptions.PersistenceException:
Error querying database. Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause:
java.lang.ClassNotFoundException: Cannot find class: sql.cj.jdbc.Driver
The error may exist in com/lz/l
The error may involve com.lz.dao.IUser.findAll
The error occurred while executing a query
步骤;
项⽬的分层
1. 创建MySQL表 ,随便创建⼀个User表就⾏;
2. 在File中新建⼀个maven⼯程 只需下⼀步,不需要使⽤任何模板;
3. 在l中添加依赖 ;
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId&batis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
5. 在src->main->java下创建包和实体类User我这⾥是com.lz.domain.User.class。其中 set,get,tostring⽅法要全;
public class User {
private Integer uid;
mysql入门基础教程private String uname;
private Integer upass;
private Integer uphone;
public User(){
}
public User(Integer uid, String uname, Integer upass, Integer uphone){ this.uid = uid;
this.uname = uname;
this.upass = upass;
this.uphone = uphone;
}
public Integer getUid(){
return uid;
}
public void setUid(Integer uid){
this.uid = uid;
}
public String getUname(){
return uname;
}
public void setUname(String uname){
this.uname = uname;
}
public Integer getUpass(){
return upass;
}
public void setUpass(Integer upass){
this.upass = upass;
}
public Integer getUphone(){
return uphone;
}
public void setUphone(Integer uphone){
this.uphone = uphone;
}
@Override
public String toString(){
return"User{"+
"uid="+ uid +
", uname='"+ uname +'\''+
", upass="+ upass +
", uphone="+ uphone +
'}';
}
}
7. 创建实体类User的⽅法接⼝IUserDao
import com.lz.domain.User;
import java.util.List;
public interface IUser {
dw图片轮播代码List<User>findAll();
}
。xml
9. 在resources根⽬录下创建SQLMapConfing ;
这⾥要换成⾃⼰的数据库名和密码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-////DTD Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
<configuration>
<!--配置环境-->
<environments default="mysql">
<!--配置mysql环境-->
<environment id="mysql">
<!--配置事物类型-->
<transactionManager type="JDBC"></transactionManager>
revolve是正品吗<!--配置数据源(连接池)-->
<dataSource type="POOLED">
<!--配置连接数据库的基本信息-->
<property name="driver" value="sql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="12345"/>
</dataSource>
</environment>
</environments>
<!--指定映射配置⽂件的位置(xml配置⽅式⽤于resource来指定)-->
<mappers>
<mapper resource="com/lz/l"/>
</mappers>
</configuration>
11. 同样在resources根⽬录下创建com.lz.l ;
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-////DTD Mapper 3.0//EN"
uml图书馆管理系统类图"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lz.dao.IUser">
<select id="findAll" resultType="com.lz.domain.User">
select * from user
</select>
</mapper>
13. 添加log4j的配置⽂件log4j.properties;
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t](%F:%L)-%m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=5
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c -%m%n
15. 编写测试类Test,最好在test->java下
import com.lz.dao.IUser;
import com.lz.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
易语言和python写脚本哪个好import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;
public class Test {
public static void main(String[] args)throws IOException, SQLException {
//读取配置⽂件
InputStream in = ResourceAsStream("l");
//创建SqlSessionFactory⼯⼚
SqlSessionFactoryBuilder builder =new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
/
/使⽤⼯⼚⽣产SqlSession对象
SqlSession sqlSession = factory.openSession();
//使⽤Sqlsession创建Dao接⼝的代理对象
IUser iUser = Mapper(IUser.class);
//使⽤代理对象执⾏⽅法
List<User> list = iUser.findAll();
for(User user : list){
System.out.String());
}
//释放资源
sqlSession.close();
in.close();
}}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论