工具:myeclipse + jboss7 + mysql
jar包:nutz-1.b.51.jar (Nutz Jar包)、mysql-connector-java-5.1.6-bin.jar (MySQL JDBC驱动)、druid-0.2.2.jar (数据库连接池)、commons-logging-1.1.1.jar (日志)
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>nutzDemo</display-name>
<filter>
<filter-name>nutz</filter-name>
<filter-class>org.nutz.mvc.NutFilter</filter-class>
<init-param>
<param-name>modules</param-name>
<param-value>com.ray.InitNutz</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>nutz</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
package com.ray;
import org.nutz.mvc.annotation.IocBy;
import org.nutz.mvc.annotation.Modules;
import org.nutz.mvc.annotation.SetupBy;
import org.nutz.mvc.ioc.provider.ComboIocProvider;
@Modules(scanPackage=true)
@IocBy(type=ComboIocProvider.class,args={
"*org.nutz.ioc.loader.json.JsonLoader","ioc/",
"*org.nutz.ioc.loader.annotation.AnnotationIocLoader","com.ray"
})
@SetupBy(value=RaySetup.class)
public class InitNutz {
}
package com.ray;
import org.nutz.dao.Dao;
import org.nutz.dao.entity.annotation.Table;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.Setup;
import org.nutz.resource.Scans;
public class RaySetup implements Setup {
private static final Log log = Logs.get();
@Override
public void init(NutConfig nc) {
// TODO Auto-generated method stub
log.debug("config Ioc = " + nc.getIoc());
Dao dao = nc.getIoc().get(Dao.class);
//如果表不存在,则创建表
for(Class<?> clazz : Scans.me().scanPackage("com.ray")) {
if(clazz.getAnnotation(Table.class) != null) {
dao.create(clazz, false);
}
}
}
@Override
public void destroy(NutConfig nc) {
// TODO Auto-generated method stub
}
}
User.java
package com.ray.model;
import org.nutz.dao.entity.annotation.ColDefine;
import org.nutz.dao.entity.annotation.Name;
import org.nutz.dao.entity.annotation.Table;
@Table("t_user")<span style="white-space:pre"> </span>//数据库表名
public class User {
@Name<span style="white-space:pre"> </span>//字符型主键或唯一索引
private String userName;
@ColDefine(notNull=true)<span style="white-space:pre"> </span>//字段详细设置
private String userPass;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPass() {
return userPass;
}
public void setUserPass(String userPass) {
this.userPass = userPass;
}
}
UserAction.java
package com.ray.action;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
@IocBean
@At("/user")<span style="white-space:pre"> </span>//namespace = /user
public class UserAction {
private static Log log = Logs.get();
@Inject
private Dao dao;
@At("/login")<span style="white-space:pre"> </span>//访问地址为 /user/login
@Ok("json")<span style="white-space:pre"> </span>//返回json
public boolean login(@Param("uid")String userName,@Param("upw")String userPass){
boolean isSuccess = false;
if(userName != null && userPass != null && userName.equals("chenlei") && userPass.equals("admin")) {
isSuccess = true;
}
return isSuccess;
}
}
conf/ioc/dao.js
var ioc = {
dataSource : {
type : "com.alibaba.druid.pool.DruidDataSource",<span style="white-space:pre"> </span>//连接池
fields : {
driverClassName : "com.mysql.jdbc.Driver",<span style="white-space:pre"> </span>//jdbc驱动
url : "jdbc:mysql://localhost:3306/db_test",<span style="white-space:pre"> </span>//数据库地址
username : "root"<span style="white-space:pre"> </span>//用户名
password : "admin"<span style="white-space:pre"> </span>//用户密码
}
},
dao : {
type : "org.nutz.dao.impl.NutDao",
args : [{refer : "dataSource"}]
}
};