MyBatis 实战
数据库
文件目录:
代码:User.xml(配置文件)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://myb atis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="test">
<select id="getUserById" parameterType="int" resultType="com.neuedu.model.User">
select * from user where usersid = #{usersid}
</select>
<select id="getAllUsers" resultType="com.neuedu.model.User">
select * from user
</select>
</mapper>
sqlMapConfig.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>
<!-- 和spring整合后 environments配置将废除-->
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理,事务控制由mybatis-->
<transactionManager type="JDBC" />
<!-- 数据库连接池,由mybatis管理-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/exam" />
<property name="username" value="root" />
<property name="password" value="439901" />
</dataSource>
</environment>
</environments>
<!-- 加载 映射文件 -->
<mappers>
<mapper resource="sqlMap/User.xml"></mapper>
</mappers>
</configuration>
User.java
package com.neuedu.model;
public class User {
private int usersid;
private String username;
private String password;
private int phone;
private String email;
public int getUserid() {
return usersid;
}
public void setUserid(int userid) {
this.usersid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [usersid=" + usersid + ", username=" + username + ", password=" + password + ", phone=" + phone
+ ", email=" + email + "]";
}
}
UserTest.java
package com.neuedu.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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 com.neuedu.model.User;
public class UserTest {
public static void main(String[] args) throws IOException {
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = ssf.openSession();
/* User user =(User)session.selectOne("test.getUserById",1);
System.out.println(user);*/
List<User> users = session.selectList("test.getAllUsers");
for(User user : users) {
System.out.println(user);
}
}
}
jdbc.properties
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=oaec
jdbc.password=oaec
log4j.properties
# Global logging configuration
#\u5728\u5f00\u53d1\u73af\u5883\u4e0b\u65e5\u5fd7\u7ea7\u522b\u8981\u8bbe\u7f6e\u6210DEBUG\uff0c\u751f\u4ea7\u73af\u5883\u8bbe\u7f6e\u6210info\u6216error
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
输出:
二、mybatis_Mapper代理方法
文件目录:
代码:User.xml(配置文件)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://myb atis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.neuedu.mapper.UserMapper">
<select id="getUserById" parameterType="int" resultType="com.neuedu.model.User">
select * from user where usersid = #{usersid}
</select>
<select id ="getAllUsers" resultType="com.neuedu.model.User">
select * from user
</select>
</mapper>
sqlMapConfig.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>
<!-- 和spring整合后 environments配置将废除-->
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理,事务控制由mybatis-->
<transactionManager type="JDBC" />
<!-- 数据库连接池,由mybatis管理-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/exam" />
<property name="username" value="root" />
<property name="password" value="439901" />
</dataSource>
</environment>
</environments>
<!-- 加载 映射文件 -->
<mappers>
<mapper resource="sqlMap/User.xml"></mapper>
</mappers>
</configuration>
User.java
package com.neuedu.model;
public class User {
private int usersid;
private String username;
private String password;
private int phone;
private String email;
public int getUserid() {
return usersid;
}
public void setUserid(int userid) {
this.usersid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [usersid=" + usersid + ", username=" + username + ", password=" + password + ", phone=" + phone
+ ", email=" + email + "]";
}
}
UserMapper.java
package com.neuedu.mapper;
import java.util.List;
import com.neuedu.model.User;
public interface UserMapper {
public User getUserById(int usersid);
public List<User> getAllUsers();
}
UserMapperTest.java
package com.neuedu.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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 com.neuedu.mapper.UserMapper;
import com.neuedu.model.User;
public class UserMapperTest {
public static void main(String[] args) throws IOException {
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = ssf.openSession();
UserMapper mapper = session.getMapper(UserMapper.class);
/*User user = mapper.getUserById(1);
System.out.println(user);*/
List<User> users = mapper.getAllUsers();
for(User user : users) {
System.out.println(user);
}
}
}
UserTest.java
package com.neuedu.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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 com.neuedu.model.User;
public class UserTest {
public static void main(String[] args) throws IOException {
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = ssf.openSession();
/* User user =(User)session.selectOne("test.getUserById",1);
System.out.println(user);*/
List<User> users = session.selectList("test.getAllUsers");
for(User user : users) {
System.out.println(user);
}
}
}
输出: