entitypackage com.ferry.cc.entity;
import java.io.Serializable;
public class CcNum implements Serializable {
private int id;
private int num;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
Daopackage com.ferry.cc.dao;import com.ferry.cc.entity.CcNum;public interface CcNumDao {
public CcNum getCcNum(int num);
public int insertNum(CcNum num);
}
mapping<?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="com.ferry.cc.dao.CcNumDao" >
<select id="getCcNum" parameterType="int" resultType="CcNum">
select * from CcNum where num =#{num};
</select>
<insert id="insertNum" parameterType="CcNum" useGeneratedKeys="true"
keyProperty="id">
insert into CcNum (num) values(#{num});
</insert>
</mapper>
server.javapackage com.ferry.cc.service;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.ferry.cc.dao.CcNumDao;
import com.ferry.cc.entity.CcNum;
import com.ferry.cc.service.serviceInfo.CcViewServiceInfo;
import com.ferry.util.MyBatisUtils;
public class CcViewService implements CcViewServiceInfo {
static SqlSessionFactory sqlSessionFactory = null;
static {
sqlSessionFactory = MyBatisUtils.getSqlSessionFactory();
}
public static void main(String[] args) throws Exception {
CcNum c = new CcNum();
c.setNum(1);
CcNum c1 = new CcViewService().getCcNum(c);
}
@Override
public CcNum getCcNum(CcNum num) throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
CcNum ccNum = null;
try {
CcNumDao userMapper = sqlSession.getMapper(CcNumDao.class);
ccNum = new CcNum();
ccNum = userMapper.getCcNum(1);
System.out.println(ccNum.getId());
sqlSession.commit();//杩欓噷涓�畾瑕佹彁浜わ紝涓嶇劧鏁版嵁杩涗笉鍘绘暟鎹簱涓�
} finally {
sqlSession.close();
}
return ccNum;
}
@Override
public int insertNum(CcNum num) throws Exception {
return 0;
}
}
MyBatisUtil.javapackage com.ferry.util;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyBatisUtils {
/**
mybatis工具类,提供openSesison,对外提供SqlSession对象。
将此对象的构造过程隐藏起来,SqlSessionFactory对象,只需要在类加载时创建出来就OK了。
需要指定mybatis的配置文件xml的路径或相应的字节流或字符流。
这里用字节流InputStream,采用类加载器的方式来获取输入字节流。
* @author 陈淑飞
* @time Oct 6, 2012
*/
private static SqlSessionFactory factory ;
static{
InputStream in = MyBatisUtils.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
factory = new SqlSessionFactoryBuilder().build(in);
}
public static SqlSessionFactory getSqlSessionFactory(){
return factory;
}
/**
返回一个SqlSession对象(每次返回一个新的SqlSession对象)
若涉及多个表的操作,涉及事务的,要做到操作失败时回滚,那么建议自定义一个TransactionUtils的工具类
用ThreadLocal类来保存SqlSession类,这样跨多个dao操作时,确保获取的都是同一SqlSession对象。然后在service层中捕获异常,再catch上用session的回滚。
* @return
*/
public static SqlSession openSession(){
return factory.openSession();
}
public static void main(String[] args) {
MyBatisUtils.openSession();
}
}
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"> <!-- dtd约束,一定要拷过去啊。下面的根据具体应用,改改就可以了 -->
<configuration>
<!-- 指定与数据库相关的配置资源文件名,若下面的dataSource所需要的url等直接配置编码在此文件,那么此properties也可以去掉 -->
<properties resource="db.properties"/>
<!-- 给指定的类定义别名,这样在后面的Mapping映射文件中,可以直接写别名,可以不用写完整限定类名了 -->
<typeAliases>
<typeAlias alias="CcNum" type="com.ferry.cc.entity.CcNum" /> <!-- 指定com.test.domain.Person用Person别名替代 -->
</typeAliases>
<environments default="default">
<environment id="default">
<transactionManager type="JDBC" /> <!-- JDBC事务管理 -->
<dataSource type="POOLED"> <!-- 数据源 连接池相关 所需要数据库连接的相关配置信息,在db.properties有指定 -->
<property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /><!-- ${driver} -->
<property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=mydb" /><!-- ${url }-->
<property name="username" value="sa" /><!-- ${username} -->
<property name="password" value="1234" /><!-- ${password} -->
</dataSource>
</environment>
</environments>
<!-- 映射文件 将sql、POJO、dao层映射连接在一起,以可配置的方式来组织。从此SQL的写法与dao层无关了。 -->
<mappers>
<mapper resource="com/ferry/cc/dao/mapping/CcNumDaoMapper.xml" />
</mappers>
</configuration>
db.properties
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc.microsoft:sqlserver://localhost:1433;DatabaseName=mydb
username=sa
password=1234
com.microsoft.sqlserver.jdbc.SQLServerException: 通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。错误:“Connection refused: connect。请验证连接属性,并检查 SQL Server 的实例正在主机上运行,且在此端口接受 TCP/IP 连接,还要确保防火墙没有阻止到此端口的 TCP 连接。”。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1049)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at LoginInfo.NewConnect(LoginInfo.java:59)
at LoginInfo.SearchOperator(LoginInfo.java:179)
at login$1.actionPerformed(login.java:151)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at LoginInfo.SearchOperator(LoginInfo.java:182)
at login$1.actionPerformed(login.java:151)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
解决方案:
右键我的电脑→管理→服务和应用程序→SQL Server 配置管理器→SQL Server 网络配置,启用 与服务器名称相同的协议并双击 ,双击TCP/IP,"IP地址"选项卡下的"IPALL",将TCP动态端口设置为0(即启用),TCP端口是1433.重启服务,最好是重启计算机下
本文详细介绍了Java开发中使用实体类、数据访问层和MyBatis进行数据库操作的过程,包括代码实现、XML配置、SQL映射以及事务管理。重点展示了如何通过MyBatis实现数据库查询和插入操作,以及如何利用SqlSessionFactory管理数据库会话。
3万+

被折叠的 条评论
为什么被折叠?



