Mybatis;
Posted 如果能够
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis;相关的知识,希望对你有一定的参考价值。
最近刚刚接受一些框架知识,Mybatis,xml的各种配置,以及各种各样的不知道是什么的文件,向我铺天盖地的砸了过来,说实在的,真是有点让人措手不及。
所以,这些代码希望能对初识Mybatis的小菜鸟们有所帮助。
首先是xml的配置文件:(这是新建的一个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="conf/jdbc_oracle.properties"></properties>
<!-- 别名 -->
<typeAliases>
<!-- <typeAlias type="model.User" alias="User"/>实体类少的情况下可用 -->
<package name="model"></package>
</typeAliases>
<!-- 开发环境 -->
<environments default="development">
<!-- 子属性 -->
<environment id="development">
<!-- 事务管理器
两种,一种是JDBC
另一种是MANAGER。。(托管)
welogic,jboss...
tomcat没有第二种
-->
<transactionManager type="JDBC"></transactionManager>
<!--
配置数据源
UNPOOLED 非连接池
POOLED 连接池
JNDI 应用服务器
-->
<dataSource type="POOLED">
<!-- 使用属性文件里面的值,或者是直接在配置文件里边定义 -->
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
</dataSource>
</environment>
</environments>
<!-- 添加映射文件 -->
<mappers>
<mapper resource="aaa/UserMapper.xml"/>
</mappers>
</configuration>
数据库的链接:(新建一个File,然后加一个后缀 .properties 这种File文件,你将后缀写成什么样子,它就会是一个什么样的文件)
jdbc.username=test
jdbc.password=199715
jdbc.driver=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
实例化一个类:(就是你的数据库里表的字段名)
package model;
public class Users {
public Integer id;
public String username;
public String password;
public Users() {
super(); }
public Users(Integer id,String username,String password){
super();
this.id=id;
this.username=username;
this.password=password; }
public Users(String username,String password){
super();
this.username=username;
this.password=password; }
public Integer getId() {
return id; }
public void setId(Integer id) {
this.id = id; }
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; }
@Override
public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + "]"; } }
重点问题:在上一篇博客中强调的问题,这个实例化的类,一定不要定义成静态的
获取链接:
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 MyBatisutill {
private static SqlSessionFactory SqlSessionFactory;
private static SqlSession sqlSession;
public static SqlSessionFactory getSqlSessionFactory(){
String path = "conf/NewFile.xml";
InputStream in = null;
try { in = Resources.getResourceAsStream(path);
} catch (IOException e) {
e.printStackTrace(); }
SqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
return SqlSessionFactory; }
public static SqlSession getSqlSession(){
if(sqlSession == null){
sqlSession = getSqlSessionFactory().openSession();
}
return sqlSession; }
public static void destory(){
sqlSession.commit();
sqlSession.close(); }
public static void main(String[] args) {
System.out.println(getSqlSession()); } }
测试:(新建一个junitTestTest)
import static org.junit.Assert.*;
import java.util.List;
import org.apache.ibatis.session.SqlSession; import org.junit.After; import org.junit.Before; import org.junit.Test;
import Utill.MyBatisutill; import aaa.User; import model.Users;
public class junitTest {
private SqlSession ss; private User um;
@Test//注解
public void test() {
/*List<Object> list = ss.selectList("aaa.User.getAllUser");
System.out.println(list);*/
/*Users users = new Users("huanghuan","111");
int a = ss.insert("aaa.User.addUser",users);
System.out.println(a);*/
/*int a = um.delUser(1);
System.out.println(a);*/
Users users = new Users("荔枝烂了","555");
int a = ss.update("aaa.User.updateUser",users); }
@Before
public void setUp() throws Exception {
ss = MyBatisutill.getSqlSession();
um = ss.getMapper(User.class);
System.out.println(ss); }
@After
public void tearDown() throws Exception {
MyBatisutill.destory(); }
}
写一个方法类,调用里边的方法;
import java.util.List;
public interface User {
public List<User> getAllUsers();
public int addUser();
public int delUser(int id);
public int updateUser();
}
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="aaa.User">
<select id="getAllUser" resultType="Users">
select * from z_user
</select>
<insert id="addUser" parameterType="Users">
insert into z_user z values(test_sq.nextval,#{username},#{password})
</insert>
<delete id="delUser" parameterType="int">
delete z_user z where z.id=#{id}
</delete>
<update id="updateUser" parameterType="Users">
update z_user z set z.username=#{username},z.password=#{password} where z.id=2
</update>
</mapper>
以上是关于Mybatis;的主要内容,如果未能解决你的问题,请参考以下文章