mybatis的第一個程序
阿新 • • 發佈:2017-12-04
host import int localhost stub 數據庫名 nco 測試 style
1,用eclipse創建一個web項目
2,導入jar包,名字就是紅框標記的
3,數據庫架構
簡單的一張mysql表
4,創建dao service entity
總覽
UserMapper.java
package cn.abc.dao; import cn.abc.entity.User; public interface UserMapper { /** * 通過id查找User * * @return */ public User getUserById(int id); }
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"> <mapper namespace="cn.abc.dao.UserMapper"> <select id="getUserById" parameterType="Integer" resultType="User"> select * from t_user where id = #{id};</select> </mapper>
User.java
package cn.abc.entity; public class User { private int id; private String name; private String password; public User() { super(); } public User(String name, String password) { super(); this.name = name;this.password = password; } public User(int id, String name, String password) { super(); this.id = id; this.name = name; this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", password=" + password + "]"; } }
UserService.java
package cn.abc.service; import cn.abc.entity.User; public interface UserService { /** * 通過id查找User * * @return */ public User getUserById(int id); }
UserServiceImpl.java
package cn.abc.service.impl; import cn.abc.dao.UserMapper; import cn.abc.entity.User; import cn.abc.service.UserService; public class UserServiceImpl implements UserService{ //定義接口對象 private UserMapper userMapper; @Override public User getUserById(int id) { // TODO Auto-generated method stub return userMapper.getUserById(id); } }
5,在src上創建source文件夾resources,並放入以下文件
跟src是平級的 不是普通的文件夾,
jdbc.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/db_ssm?useUnicode=true&characterEncoding=utf-8 user=root password=myw123456
log4j.properties
log4j.rootLogger=DEBUG,CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%p] %d %c - %m%n
mybatis-config.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> <properties resource="jdbc.properties"/> <typeAliases> <!-- <typeAlias alias="Student" type="com.abc.entity.Student"/> --> <package name="cn.abc.entity"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${user}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments> <mappers> <!-- <mapper resource="com/abc/mappers/StudentMapper.xml" /> --> <package name="cn.abc.dao"/> </mappers> </configuration>
6,mybatis工具類 MybatisUtil
代碼
package cn.abc.util; import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MybatisUtil { private static SqlSessionFactory sqlSessionFactory; public static SqlSessionFactory getSqlSessionFactory() { try { InputStream in = Resources.getResourceAsStream("mybatis-config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(in); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("加載文件出錯"); e.printStackTrace(); } return sqlSessionFactory; } public static SqlSession openSession() { return getSqlSessionFactory().openSession(); } }
7,測試類 創建junit測試類 Test001.java 註意,此處要導入junitjar包,不再貼圖,因為eclipse創建junit時會自己導包的
代碼
package cn.abc.test; import static org.junit.Assert.*; import org.apache.ibatis.session.SqlSession; import org.apache.log4j.Logger; import org.junit.After; import org.junit.Before; import org.junit.Test; import cn.abc.dao.UserMapper; import cn.abc.entity.User; import cn.abc.util.MybatisUtil; public class Test001 { private static Logger logger = Logger.getLogger(Test001.class); private SqlSession sqlSession = null; private UserMapper userMapper = null; @Before public void setUp() throws Exception { sqlSession = MybatisUtil.openSession(); userMapper = sqlSession.getMapper(UserMapper.class); } @After public void tearDown() throws Exception { sqlSession.close(); } @Test public void test() { User user = userMapper.getUserById(1); System.out.println(user.toString()); } }
運行結果
name怎麽是null呢。經查找是數據庫名稱與類的名稱不匹配所致。
8,解決錯誤
修改數據庫中的屬性
修改前
修改後
註意,修改了name
重新運行test001.java測試
ok,成功了。出現錯誤的原因是實體類與數據庫不匹配所致,以後要多加註意。
mybatis的第一個程序