这篇文章主要介绍使用mybatis时会有哪些基础错误,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!一、使用Maven加载依赖加载了连接数据库的依赖、mybatis的依赖以及lombok的依赖<dependenc
这篇文章主要介绍使用mybatis时会有哪些基础错误,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
加载了连接数据库的依赖、mybatis的依赖以及lombok的依赖
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>Mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.4</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency>
<?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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/数据库名字?/useUnicode=true&characterEncoding=utf8&useSSL=false" /> <property name="username" value="root" /> <property name="passWord" value="数据库密码" /> </dataSource> </environment> </environments> </configuration>
@Data@NoArgsConstructor@AllArgsConstructorpublic class User { private Integer uid; private String uname; private String password;}
见名知意,操作的user表,所以映射文件也命名为了userMapper.xml
<?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"><!--tuser是我自己定的名字--><mapper namespace="tuser"> <!--id自己设置,等会调用方法名使用,resultType查询结果类型--> <select id="getUserById" resultType="com.hongda.space.entity.User"> <!--sql语句,#{}是参数格式,注意字段要与实体类属性一致--> select * from t_user where uid=#{uid} </select> <!--parameterType参数类型--> <insert id="insertUser" parameterType="com.hongda.space.entity.User"> insert into t_user(uname,password) values(#{uname},#{password}); </insert></mapper>
将编写好的userMapper.xml添加到mybatis.xml配置文件,下方
<?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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/数据库名字?/useUnicode=true&characterEncoding=utf8&useSSL=false" /> <property name="username" value="root" /> <property name="password" value="数据库密码" /> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/userMapper.xml"/> </mappers></configuration>
import com.hongda.space.entity.User;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;public class Test01 { public static void main(String[] args) throws IOException { //1.读取配置文件 Reader reader = Resources.getResourceAsReader("conf.xml"); //2.获取sqlSessionFactory SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); //3.获取sqlsession对象 执行sql SqlSession session = sessionFactory.openSession(); //4.执行sql User user = session.selectOne("tuser.getUserById", 1); System.out.println(user); int i = session.insert("tuser.insertUser",new User(null, "小刘", "4545")); System.out.println(i+"------------"); session.commit(); }}
结果:
编写中可能碰到的错误
Mapped Statements collection does not contain…
The server time zone value'XXXXXXXXX' is…
在Mybatis配置文件中的jdbc连接地址后面拼接serverTimezone=Asia/Shanghai
以上是“使用mybatis时会有哪些基础错误”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网精选频道!
--结束END--
本文标题: 使用mybatis时会有哪些基础错误
本文链接: https://lsjlt.com/news/268748.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0