您好,登錄后才能下訂單哦!
這篇文章主要介紹使用mybatis時(shí)會(huì)有哪些基礎(chǔ)錯(cuò)誤,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
加載了連接數(shù)據(jù)庫(kù)的依賴、mybatis的依賴以及l(fā)ombok的依賴
<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/數(shù)據(jù)庫(kù)名字?/useUnicode=true&characterEncoding=utf8&useSSL=false" /> <property name="username" value="root" /> <property name="password" value="數(shù)據(jù)庫(kù)密碼" /> </dataSource> </environment> </environments> </configuration>
@Data @NoArgsConstructor @AllArgsConstructor public 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自己設(shè)置,等會(huì)調(diào)用方法名使用,resultType查詢結(jié)果類型--> <select id="getUserById" resultType="com.hongda.space.entity.User"> <!--sql語(yǔ)句,#{}是參數(shù)格式,注意字段要與實(shí)體類屬性一致--> select * from t_user where uid=#{uid} </select> <!--parameterType參數(shù)類型--> <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/數(shù)據(jù)庫(kù)名字?/useUnicode=true&characterEncoding=utf8&useSSL=false" /> <property name="username" value="root" /> <property name="password" value="數(shù)據(jù)庫(kù)密碼" /> </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; /** * @ClassName Test01 * @Description TODO * @Author whd * @Date 2021/3/19 14:45 */ 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對(duì)象 執(zhí)行sql SqlSession session = sessionFactory.openSession(); //4.執(zhí)行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(); } }
結(jié)果:
編寫中可能碰到的錯(cuò)誤
1.Mapped Statements collection does not contain…
2.The server time zone value'XXXXXXXXX' is…
在Mybatis配置文件中的jdbc連接地址后面拼接serverTimezone=Asia/Shanghai
以上是“使用mybatis時(shí)會(huì)有哪些基礎(chǔ)錯(cuò)誤”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。